- Home /
How to change velocity directly to a certain speed while keeping the collide direction?
I have a falling object colliding with a "Power Cube". This cube needs to get the velocity of the other rigidbody on collision and change the velocity of that object directly to a defined speed in a variable boostSpeed, while still keeping the same bounce direction as how a rigidbody would handle this.
I came up with this code but its not working correctly for me:
public class PowerCube : MonoBehaviour {
public float boostSpeed;
void OnCollisionEnter(Collision other) {
// Power Cube actions
other.rigidbody.velocity = other.rigidbody.velocity * boostSpeed;
}
}
This will increase the velocity but based on the current velocity of the object. I want all objects that collide with the power cube have the same velocity speed after they collide, but still in the direction how a normal rigidbody would respond. How can I do this?
Answer by Jeff-Kesselman · May 13, 2014 at 09:44 PM
Velocity is a Vector.
A vector by definition is a magnitude and a direction.
If you normalize the vector, you get the direction with a magnitude of 1
If you multiply a normalized vector times a scalar N, you get a vector in the same direction with a magnitude of N.
This is why you really need to understand Vector math to do 3D games. If you do, such things are obvious.