Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
2
Question by Zipball · Dec 01, 2011 at 09:32 PM · rigidbodyjumpnoob

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);
 }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by syclamoth · Dec 01, 2011 at 09:34 PM

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image c44help · Nov 26, 2020 at 08:38 PM 0
Share

Thank you it works very well with these two lines of codes.

avatar image
1

Answer by aldonaletto · Dec 01, 2011 at 09:38 PM

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; } }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vincent P · Jan 27, 2013 at 04:45 PM 1
Share

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

avatar image aldonaletto · Jan 28, 2013 at 12:25 PM 0
Share

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.

avatar image Vincent P · Jan 30, 2013 at 05:23 AM 0
Share

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.

avatar image
0

Answer by XenoTracker · Apr 09, 2015 at 04:37 AM

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NikkiH · Mar 14, 2016 at 08:53 PM 0
Share

void* without a capital and public* without a capital

avatar image Unity_scat · May 06, 2016 at 01:25 AM 0
Share

@XenoTracker Thank you for the code!

avatar image meat5000 ♦ · May 06, 2016 at 07:43 AM 0
Share

Nothing new. Just there is no rigidbody shortcut any more so you need to link it with GetComponent like any other component now.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Later grab doesn't jump 0 Answers

Rigidbody Stop and jump 0 Answers

c# jumping isn't consistent, please help 0 Answers

Character stuck in ground/can't get character to jump 1 Answer

So I implemented a Wallrun into my PlayerMovement Script, but I need to press A or D to activate it. How to I make it automatic? (Script below, it's based off of rigidbody rather than CharacterController) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges