2
\$\begingroup\$

I am working on a platform game,When I press the space bar the object should jump and when I release the space bar it should come down.

The code I have used for jump is

void Update () {
    transform.Translate(Vector3.right*Time.deltaTime);
    if(Input.GetKey(KeyCode.Space))
    {
        rigidbody2D.AddForce(Vector3.up*3);
    }

    if(Input.GetKeyUp(KeyCode.Space))
       {
        rigidbody2D.AddForce(Vector3.down);
    }

}

But when I release the space bar the object is not coming down

\$\endgroup\$
  • \$\begingroup\$ try increasing the mass \$\endgroup\$ – The Light Spark Nov 2 '15 at 13:48
  • \$\begingroup\$ Which version of Unity are you using? because in Unity 5, you wouldn't be able to directly use rigidbody2D. You'd do GetComponent<Rigidbody2D>.AddForce() \$\endgroup\$ – SanSolo Nov 2 '15 at 14:44
  • 1
    \$\begingroup\$ Does increasing the downwards force to Vector3.down*4 (or more) help? \$\endgroup\$ – Aaganrmu Nov 2 '15 at 16:37
0
\$\begingroup\$

To make your code work, you should set gravity to 0 in inspector and reset Rigitbody velocity to zero each time before adding force:

    transform.Translate(Vector3.right * Time.deltaTime/5f);
    if (Input.GetKey(KeyCode.Space))
    {
        rigidbody2D.velocity = Vector2.zero;
        rigidbody2D.AddForce(Vector3.up,ForceMode2D.Impulse);
    }

    if (Input.GetKeyUp(KeyCode.Space))
    {
        rigidbody2D.velocity = Vector2.zero;
        rigidbody2D.AddForce(Vector3.down,ForceMode2D.Impulse);
    }`

Or you can let the gravity do all the job. Just add force up only, and when button is not pressed gravity will move your object down.

\$\endgroup\$
3
\$\begingroup\$

Use Input.GetKeyDown() . GetKey() only returns for the first frame when you pressed the key.

\$\endgroup\$
  • \$\begingroup\$ Its not working still \$\endgroup\$ – user1509674 Nov 2 '15 at 12:16
3
\$\begingroup\$

You have (at least) two fundamental problems. I'm assuming here that you're not using any friction or gravity, but I'll come back to that later.

The most obvious thing is that your upward force is 3x your downward force. Basic physics tells us that, if you apply a force of 3x to an object to start its movement, it'll take a force of 3x in the opposite direction just to stop it moving. You're applying 1/3 of the force necessary to stop the object, which isn't nearly enough to start it moving in the opposite direction.

The other thing that's happening (as others have noted) is that you're applying an even bigger upward force than you realise, because you're using GetKey. GetKey will return true for every frame that the button is held and so, if you press the key for 1/10 of a second and you're running 60FPS, you'll apply the force 6 times. When you release the key and GetKeyUp is true, it's only true for a single frame. That's skewing the balance of your forces even further.

So, probably, you need to switch to using GetKeyDown instead of GetKey, and make the downward force Vector3.down * 6. Then, you would hopefully see a change in direction.

However: it's worth noting at this point that you're using the physics engine here to handle your jumping, and the physics engine is capable of applying gravity and friction to your object. This could make your fall much easier to implement: simply stop applying the upward force, and your object will fall back to the ground!

\$\endgroup\$
0
\$\begingroup\$

You're method is fine, but you're adding too little force for it to jump. Assuming you have default gravity settings, try this :

rb.AddForce (Vector3.up*30); and you will see the object jumping.

For clarification, rb is reference to Rigidbody2D. In my Awake() method, I have: rb=GetComponent<>(Rigidbody2D);

\$\endgroup\$
0
\$\begingroup\$
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
        transform.Translate(Vector3.up * Time.deltaTime*100);
        //Debug.Log("Clicked");
            }
    if (Input.GetKeyUp (KeyCode.Space)) {
        //transform.position=initialposition;
        transform.Translate(Vector3.down * Time.deltaTime*100);
        Debug.Log("Clicked");
    }
    if(Input.GetKey(KeyCode.RightArrow))
    {
        transform.Translate(Vector2.right*Time.deltaTime);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.