x


Make a rigidbody Jump (global up)

Hi, I am attempting to make a rigid body jump upwards (global) The script which i have tried to use doesn't seem to work. All help is appreciated.

    function Update(){
    if (Input.GetKeyDown (KeyCode.Space)){

    rigidbody.AddForce (0, 10, 0);
}
}
more ▼

asked Dec 01 '11 at 09:32 PM

Zipball gravatar image

Zipball
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

First off, put that in FixedUpdate. Otherwise, try using this-

rigidbody.AddForce(new Vector3(0, 100, 0), ForceMode.Impulse);

It's possible that you just aren't seeing anything because the force is too low!

more ▼

answered Dec 01 '11 at 09:34 PM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

(comments are locked)
10|3000 characters needed characters left

The easiest way is to set the rigidbody.velocity directly (you will not depend on the object mass):


var jumpSpeed: float = 8;

function Update(){
    if (Input.GetKeyDown (KeyCode.Space)){
        rigidbody.velocity += jumpSpeed * Vector3.up;
    }
}
more ▼

answered Dec 01 '11 at 09:38 PM

aldonaletto gravatar image

aldonaletto
41.4k 16 42 197

According to the docs you should not directly modify the velocity of a rigidbody as it creates weird results and unrealistic behaviour.

Jan 27 at 04:45 PM Vincent P

Yes: in this particular case, the unrealistic behaviour will be the rigidbody stopping its horizontal movement and jumping vertically. Actually, you can modify rigidbody.velocity directly without screwing up the physics as long as the current velocity is taken into account - that's what AddForce and other rigidbody functions do.
A better alternative in this case would be to add the vertical velocity like this:

rigidbody.velocity += Vector3.up * jumpSpeed;

This would preserve the current movement and simply add a vertical component.
Thanks for the comment - the answer has been edited.

Jan 28 at 12:25 PM aldonaletto

No I was more talking about the fact that if you just add velocity to rigidbody, you are not "pushing" it up. So what happens is that gravity starts to compund and as soon as you stop applying the velocity, the object will shoot down. Don't get me wrong, it'll still works. Just commenting in case any one else runs into the issue.

Jan 30 at 05:23 AM Vincent P
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1947
x1787
x335
x111

asked: Dec 01 '11 at 09:32 PM

Seen: 2210 times

Last Updated: Jan 30 at 05:23 AM