Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
2
Question by Ves · Apr 30, 2011 at 01:51 AM · rigidbodyvelocitygravity

How to keep gravity when adding velocity to Rigidbody

Hey all,

I have a rigidbody on a hill and have it face a random direction. I then want to apply a velocity to the rigidbody, but I can't see how to do this without nulling the effect of gravity. That is, if I set the y-velocity to be zero, the rigidbody just floats through the air. Is there a way to keep gravity and have velocity only in the x- and z-directions?

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
0
Best Answer

Answer by Ves · May 01, 2011 at 05:47 AM

So I figured this out and thought I would share it. It appears that whenever you set the velocity for a direction, it negates forces in that direction. I circumvented this by only ever setting the x- and z-components of the velocity vector specifically, and not touching the y-component.

If there is a more elegant way, please let me know!

Comment
Add comment · 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
4

Answer by quantumface · Mar 03, 2015 at 10:45 AM

You should NOT ever modify the velocity directly, use it as read-only.

For anyone else seeing this post after 4 years or later - whenever you AddForce in any direction, you apply exactly (added_force)/(50*rigidbody's_mass) velocity to that direction.

For example: let's say you have an object with rigidbody, a mass of 3, that stays in one place in air, and is not affected by gravity. You wanted it to start moving in X direction with velocity of 13.37.

Since velocity=(added_force)/(50*rigidbody's_mass) then to find out how much force we want to add is added_force=velocity*(50*rigidbody's_mass).

So 13.37(50*3) = 13.37*150 = 2005.5 is the amount of force you need to add.

 var wantedVel : float = 13.37; //we want 13.37 velocity

 function Start() {
     addVel(); //call our force adding function
 }

 function addVel() {
     var calculatedForce : float = wantedVel*(50*rigidbody.mass); //calculate force amount
     rigidbody.AddForce(Vector3(calculatedForce,0,0)); //add force on X

     Debug.Log("velocity: " + rigidbody.velocity.x); //debug to see final velocity
 }


 CONSOLE:
     velocity: 13.37

Now, if you need to add angled velocity, not in just one specific direction, then google all about Vector Addition, it may scare you at first, but trust me, it is very easy. Or just search Unity for adding angular force.

I hope I helped someone :D.

Comment
Add comment · Show 2 · 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 KUNGERMOoN · Mar 22 at 06:31 PM 0
Share

Thanks! Very helpful answer! You can also create some extension methods to handle adding velocity, something like that:

 public static void AddVelocity(this Rigidbody2D rigidbody, Vector2 velocity)
 {
     rigidbody.AddForce(velocity * 50 * rigidbody.mass);
 }
 
 public static void AddVelocity(this Rigidbody rigidbody, Vector2 velocity)
 {
     rigidbody.AddForce(velocity * 50 * rigidbody.mass);
 }
avatar image Eno-Khaon KUNGERMOoN · Mar 22 at 07:40 PM 0
Share

As an addendum/correction to this, multiplying by 50 solves the symptoms of a very simple problem to alleviate, and only if the physics timestep (default 0.02, or 1/50) remains unchanged.

A better solution is to change the ForceMode used by the function, as this will automatically factor in (potentially) both the physics timesteps and mass:

 rigidbody.AddForce(velocity, ForceMode.VelocityChange);

(To note: Physics2D only includes ForceModes of Force and Impulse, which do not ignore mass, while Physics (3D) also includes Acceleration and VelocityChange, which do ignore mass)

avatar image
0

Answer by theprojectabot · Jan 10, 2013 at 09:17 AM

perhaps when doing the y component, also add in the Physics.gravity amount to the y value?

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Gravitational pull without losing speed 1 Answer

How to set velocity of Rigidbody without changing gravity? 1 Answer

G Force Acceleration 0 Answers

Need help with this script 0 Answers

Gravity doesn't work 2 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