Search Unity

update custom function more than 1 time per frame

Discussion in 'Scripting' started by _rolands, Apr 4, 2014.

  1. _rolands

    _rolands

    Joined:
    Mar 16, 2014
    Posts:
    9
    Hi, I am wondering is it possible to update my own custom function more than once per frame. I know that I can configure FixedUpdate but I need something faster to return bool from Physics.CapsuleCast()
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Why do you need something called more often than FixedUpdate?
     
  3. _rolands

    _rolands

    Joined:
    Mar 16, 2014
    Posts:
    9
    Well I am struggling with a character movement script, I want to make my own one because CharacterController does not fit my need... Currently I have something (absolutely not working like I wish) I have done so far...
    I don't need physics for my character but when I use transform.Translate() it goes into other objects.

    Am I on the wrong path and should use something totally different?

    PS. Jumping still in process

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerBehaviour : MonoBehaviour {
    6.  
    7.         public Vector3 gravityScale;
    8.         public Vector3 jumpVelocity;
    9.         public float jumpHeight;
    10.  
    11.         public float movementSpeed;
    12.  
    13.         private bool grounded;
    14.         private bool jumpAvailable;
    15.         private bool jump;
    16.  
    17.         private bool obstacleInFront;
    18.  
    19.         private RaycastHit hit;
    20.         public float capsuleCastRadius;
    21.  
    22.         // Use this for initialization
    23.         void Start () {
    24.                 jumpHeight = 3f;
    25.                 gravityScale.y = -2.0f; // if time to appex = 0.35s formula: (2 * humpHeight) / timeToAppex^2
    26.                 jumpVelocity = Vector3.zero; //Mathf.Sqrt(2 * gravityScale * jumpHeight);
    27.  
    28.                 movementSpeed = 5.0f;
    29.  
    30.                 grounded = false;
    31.                 jumpAvailable = false;
    32.                 jump = false;
    33.  
    34.                 obstacleInFront = false;
    35.  
    36.                 hit = new RaycastHit();
    37.         }
    38.        
    39.         // Update is called once per frame
    40.         void Update () {
    41.                 Movement();
    42.         }
    43.  
    44.         void FixedUpdate(){
    45.                 grounded = Physics.Raycast(transform.position, Vector3.down, 1.0f);
    46.                 obstacleInFront = Physics.CapsuleCast(transform.position + (transform.up * 0.5f), transform.position + (-transform.up * 0.5f), 0.5f, Vector3.right, 0.1f);
    47.                 Debug.Log(obstacleInFront);
    48.         }
    49.  
    50.         void Movement(){
    51.                 if(!obstacleInFront){
    52.                         transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * movementSpeed * Time.deltaTime, 0, 0));
    53.                 }
    54.  
    55.                 if(grounded){
    56.                         jumpAvailable = true;
    57.                         jump = false;
    58.                         jumpVelocity = Vector3.zero;
    59.                 }
    60.  
    61.                 if(!grounded  !jump){
    62.                         jumpVelocity.y = jumpHeight;
    63.                         transform.position = transform.position + Time.deltaTime * gravityScale;
    64.                 }
    65.  
    66.                 if(jumpAvailable  Input.GetButtonDown("Jump")){
    67.                         jumpAvailable = false;
    68.                         jump = true;
    69.                         grounded = false;
    70.                 }
    71.  
    72.                 if(jump){
    73.                         transform.position = transform.position + Time.deltaTime * jumpVelocity;
    74.                         jumpVelocity = jumpVelocity + Time.deltaTime * gravityScale;
    75.                 }
    76.         }
    77. }
    78.