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

avatar image

Zipball
16 1 2 2

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

4 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

avatar image

syclamoth
19.1k 22 43 150

(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

avatar image

aldonaletto
60.6k 51 189 349

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

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
(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, 2014 at 12:53 PM

avatar 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, 2014 at 06:45 AM -GenericPath-
(comments are locked)
10|3000 characters needed characters left

Apparently this is the new AddForce thing

 Public float jumpSpeed = 5f;//or whatever you want it to be
 public Rigidbody rb; //and again, whatever you want to call it
 
 void Start (){
    rb = GetComponent <Rigidbody>();
 }
 
 Void FixedUpdate(){
    if(Input.GetKey (KeyCode.Space)){
       rb.AddForce(Vector3.up * jumpSpeed);
    }
 
 }

and in the inspector just drag the Player game object from the hierarchy to the rb slot in the script and you're good :3

more ▼

answered Apr 09 at 04:37 AM

avatar image

XenoTracker
11 1

(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:

x3598
x837
x317

asked: Dec 01, 2011 at 09:32 PM

Seen: 8654 times

Last Updated: Apr 09 at 04:37 AM