Search Unity

Unable to get Jumping Function to work consistantly

Discussion in '2D' started by Fabioswan, Apr 20, 2017.

  1. Fabioswan

    Fabioswan

    Joined:
    Apr 20, 2017
    Posts:
    1
    Hey all,

    So I was finally able to get a reliable character controller working with my Unity project.

    It all works as intended with the exception of jump. Most of the time, jump works perfectly, but on occasion the button press or release does not register to the engine. I am assuming this has to do with the Button press not occurring during a frame update but I don't have a good way to prove this.

    Can anyone give me some tips?

    Here is my current FixedUpdate function. I have tried this in Update as well but get the same issues.

    Code (CSharp):
    1.     void FixedUpdate () {
    2.         Debug.Log ("Fixed Update");
    3.         velocity.x = Input.GetAxis ("Horizontal") * 0.15f;
    4.         if(Input.GetKey("r"))
    5.         {
    6.             transform.position = new Vector3 (-5.42f, 3.89f, -0.03f);
    7.             velocity.x = 0;
    8.             velocity.y = 0;
    9.         }
    10.         if (controller.isGrounded) {
    11.             //Holds player to Ground
    12.             velocity.y = -0.001f;
    13.             //Debug.Log ("On Ground");
    14.         } else {
    15.             velocity.y -= gravity * Time.deltaTime;
    16.             //Debug.Log ("In Air");
    17.         }
    18.         if (Input.GetButtonDown ("Jump") && controller.isGrounded) {
    19.             velocity.y = .34f;
    20.         }
    21.         if(Input.GetButtonUp("Jump") && velocity.y > 0) {
    22.             velocity.y *= 0.45f;
    23.         }
    24.  
    25.         controller.move(new Vector3(velocity.x, velocity.y, 0));
    26.        
    27.     }
    Thanks,

    Rawhide
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    First, glad to hear you got it mostly working.
    Secondly, though you said Update didn't work better, I would still move the Button Up/Down detection to Update (maybe with bool variables that you can check in FixedUpdate and act accordingly , (and reset)).
    More reliable that way, imo.
    If that still doesn't 'register' that is odd, I think, and maybe something else is the matter.