- Home /
Change velocity direction
Hi
I have an Object (with rigidbody), which I move with AddForce. A lot of force is added from the beginning, then its flying and slowly falls down because of drag and gravity.
While flying I want to control the object. E.g. if I tilt the iPhone to the left it will change the direction/forward and continue in that direction.
This is not happening. It continunes the same direction as the force was given. I tried AddRelativeForce, but this doesnt work either.
My last try was to override the velocity like this:
rigidbody.velocity = transform.forward * rigidbody.velocity.magnitude;
Here the problem is the gravity which I override (I think) but the Direction is working great.
Can anybody help? :)
Answer by skovacs1 · Aug 30, 2010 at 06:25 PM
There are some solutions that come to mind immediately.
Set your velocity only on axes that don't affect gravity
You could just set the velocity on the x-z plane to face the direction of your object. This means that you are not changing speed at all, but you could if you wanted.
var lateralSpeed : float = Vector2(rigidbody.velocity.x, rigidbody.velocity.z).magnitude;
//Assumes forward is oriented correctly
rigidbody.velocity.x = transform.forward.x * lateralSpeed;
rigidbody.velocity.z = transform.forward.z * lateralSpeed;
//If forward is not correctly oriented, you will need to apply torque to reorient or
//store the direction of your x-z movement to use and change in stead
You might even want to change the speed calculated with gravity according to the buoyancy of your object relative to the matter in which it is acting (air, water, etc.) and the side that is facing that direction.
Note that setting the velocity manually will result in unnatural physics simulation.
Adding forces/torque
In all physics-function-based approaches, momentum will be conserved, meaning that any forces or changes applied will not alter the motion that was applied previously unless they are applied in an opposing direction.
AddRelativeForce will add force (given in the relative coordinate system) to existing forces. rigidbody.AddRelativeForce(Vector3(10,0,0)) is the same as rigidbody.AddForce(transform.right * 10);
If you want to actually change the forward direction, you could use torque. This becomes a pain if you want it to stop turning - a simple trick is to set freezeRotation to true if you're not turning and false if you are. This won't actually move in the new direction, but will re-orient your coordinate system so that adding force in the forward direction will actually move you in the new forward direction (but the momentum from before will still be active).
The more accurate physics-based approach is to AddForceAtPosition to add force where your force is actually coming from which will add the torque and force as appropriate. so if you added a force in the right direction at the front of your object, the object will turn to the right as dictated by the amount of force which will add to the momentum and will change the direction torque accordingly about your center of mass relative to your mass. This will obviously not diminish the momentum unless applied in an opposing direction. Having an incorrectly positioned center of mass will produce unnatural results, so you may have to set or calculate this.
Calculating movement information
Physics engines are great, but require a fair bit of understanding of the physical phenomenon you are simulating in order to work correctly for more complex physics.
A great example of this is buoyancy. If you turn on gravity, the object will accelerate at around -9.81 m/s2 on the world's y axis. If your object is in water there will be water resistance and if in the air you need to consider air resistance, meaning that it won't accelerate a -9.81 m/s2 because the matter underneath it is slowing its descent as it collides with the resisting face (Applying a force in the opposite direction), meaning that in order to compensate via the physics engine, you would need to check the velocity relative to the sides that will resist and apply compensatory force every FixedUpdate to simulate the resistance to gravity. Even more fun problems arise as an object can gain momentum by falling with a side that doesn't resist against the air, but as the object tips such that a resisting side is facing the falling direction, the momentum gained while falling is preserved as forward momentum, something which would have be emulated by adding force in the direction tipped to generate a velocity close to that gained while falling (it would lose some velocity as it tipped).
You might consider calculating your own relative drag for the sides which present greater resistance. You might even want to use a kinematic rigidbody with your own gravity, calculating your own conserved momentum and rotation(if any).
Yeah it worked great. I actually tried the same yesterday but there is one important thing to remember. X and Z rotation must be zero. I had a little X rotation X = -15 and that was enough to mesh it alle up. Works fine now with X = 0, and now the rotation and collider are in a child object.
Collision calculations works perfektly, even though I manipulate velocity directly.
Thx
Your answer
Follow this Question
Related Questions
Rigid Body relative speed 1 Answer
velocity direction and reversing it 1 Answer
How do you limit velocity on a rigid body in only one direction? 1 Answer
Throwing knife goes straight 1 Answer
Rigidbody Local Velocity 1 Answer