Hi there. I was wondering how you add momentum to a basic rigidbody movement. Does anybody know?
Rigidbodies have momentum by default, as per newtonian laws of physics. Just make sure you’re not doing something like:
- Overwriting the velocity directly each frame
- Moving via non-physical means like transform.Translate
I am using forces with my controller and it clamps the speed of the rigidbody. I just don’t know how to implement it via code
Rigidbodies have a Drag value. Anything over 0 (and less than 1 – it’s the percent of speed lost) makes them slow down (which is like killing momentum?). But a starting rigidbody has a drag of 0. so Drag isn’t your problem unless you changed it.
You’re allowed to play with an object’s speed directly. transform.GetComponent<Rigidbody>().velocity
is what the Physics system uses. It’s a simple Vector3. To give it a max speed of 8, for example, use this sneaky code. First ignore the math and just see that we can change the speed (on the last line):
Rigidbody rb = transform.GetComponent<Rigidbody>();
float speed=rb.velocity.magnitude;
if(speed>8) {
float scaleFactor=8.0f/speed; // note: will be less than 1
rb.velocity*=scaleFactor;
}