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, 2011 at 09:32 PM

Zipball gravatar image

Zipball
16 1 2 2

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

3 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, 2011 at 09:34 PM

syclamoth gravatar image

syclamoth
18.4k 20 28 118

(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, 2011 at 09:38 PM

aldonaletto gravatar image

aldonaletto
57.7k 33 94 305

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, 2013 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, 2013 at 05:23 AM Vincent P

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

Jan 27, 2013 at 04:45 PM Vincent P
(comments are locked)
10|3000 characters needed characters left

hello i have question i use this script for jumping but if i still clicking "space" i jump high and higher and highest why?

more ▼

answered Mar 24 at 12:53 PM

Japajko gravatar image

Japajko
1

This is due to the fact that you are just looking for space to be pushed and if it is it will jump. What you need to do is check to see if you are on the ground before you can actually jump.

Apr 03 at 06:45 AM -GenericPath-
(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:

x4143
x3176
x716
x280

asked: Dec 01, 2011 at 09:32 PM

Seen: 6547 times

Last Updated: Apr 03 at 06:45 AM