- Home /
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?
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!
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.
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);
}
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)
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?
Your answer
Follow this Question
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