Search Unity

{Solved} Character jumping not smooth.

Discussion in 'Scripting' started by AAcat, Nov 30, 2015.

  1. AAcat

    AAcat

    Joined:
    Apr 11, 2015
    Posts:
    19
    every time the space bar is pressed (and the player is still on the floor) the player moves up to jump. However sometime, even though hes touching the floor, he doesn't jump unless i keep mashing the space bar. Does anyone know how to make this work better? (main jumping code is in line 19-23)
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var rotationSpeed = 1000;
    4. var jumpHight = 10;
    5.  
    6. private var isFalling = false;
    7.  
    8. function OnCollisionStay ()
    9. {
    10.     isFalling = false;
    11. }
    12.  
    13. function Update ()
    14. {
    15.     var rotation : float = Input.GetAxis("Horizontal") * rotationSpeed;
    16.     rotation *= Time.deltaTime;
    17.     GetComponent.<Rigidbody>().AddRelativeTorque(Vector3.down * rotation);
    18.    
    19.     if (Input.GetKeyDown (KeyCode.Space) && isFalling == false)
    20.     {
    21.         GetComponent.<Rigidbody>().velocity.y = jumpHight;
    22.     }
    23.     isFalling = true;
    24. }
    25.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    physics "stuff" should be handled in the FixedUpdate function. Input "stuff" needs to be done in the Update function (or they might get missed).
     
  3. AAcat

    AAcat

    Joined:
    Apr 11, 2015
    Posts:
    19
    can you give a example?
     
  4. DMSolace

    DMSolace

    Joined:
    Nov 30, 2015
    Posts:
    9
    Your isFalling variable is continously cycling between true/false.

    Remove isFalling from Update and Try:
    Code (CSharp):
    1. void OnCollisionEnter()
    2. {
    3. isFalling = false;
    4. }
    5. void OnCollisionExit()
    6. {
    7. isFalling = true;
    8. }
    Also, it is much more efficient to set components to a variable in Start, instead of setting them every update frame.
     
    AAcat likes this.
  5. AAcat

    AAcat

    Joined:
    Apr 11, 2015
    Posts:
    19
    It says void is not a valid macro. "BCE0038: 'void' is not a valid macro." This is in JavaScript and you put CSharp.
     
  6. DMSolace

    DMSolace

    Joined:
    Nov 30, 2015
    Posts:
    9
    My bad, replace void with function, like: function OnCollisionEnter()
     
    AAcat likes this.