Search Unity

Inconsistent Jumps for 2d

Discussion in '2D' started by Nuk1nat0r, Sep 25, 2016.

  1. Nuk1nat0r

    Nuk1nat0r

    Joined:
    Sep 25, 2016
    Posts:
    4
    I'm using c# for unity2d, and whenever I do addforce to the game object, it always jumps at a different height. I've tried putting it in fixed update and update, and i'm not using time.deltatime either. I've also tried directly changing the velocity, but it still is inconsistent. My gravity is -9.81 and the gravity scale is 3. Mass for my object is around 2.7, but it should still be consistent, right? sorry I'm really new to this


    Code (CSharp):
    1. if (Input.GetKeyDown (KeyCode.UpArrow)) {
    2.             GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, 0);
    3.             GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, 1750), ForceMode2D.Impulse);
    4.         }
     
  2. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    Are you getting longer jumps the longer you hold the UpArrow down? I think you need a boolean that is set to True when you first start jumping but wont do your jump code if you are already jumping. You then set the boolean to false when you collide with the ground again.
     
  3. Nuk1nat0r

    Nuk1nat0r

    Joined:
    Sep 25, 2016
    Posts:
    4
    my jumps are random regardless of the length or where i initiate the jump. also if it is getkeydown should the code only execute once? hmm im confused
     
  4. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    No, you are simply checking if the Key is down at the time the code runs. So if this is being done as part of the update function you are essentially checking and running that code once per frame.
     
    Nuk1nat0r likes this.
  5. Nuk1nat0r

    Nuk1nat0r

    Joined:
    Sep 25, 2016
    Posts:
    4
    oh ok thanks!! :)