- Home /
Stoping Momentum
Hi, i'm Making a platformer Based on Momentum and i was woundering if there is a way to make it and so if i press a key it stop all momentum compleatly and so i fall back down?
Any info would be good thx
Answer by Xenon42 · Sep 04, 2016 at 03:10 PM
Try this:
if (Input.GetKey(KeyCode.E))
playerCharacter.rigidbody.velocity = 0
Take that, and slot it into your script. Replace KeyCode.E with whatever key you want, and change playerCharacter to whatever variable points to your player character's GameObject.
Oh yeah, this code will work for angularVelocity too, so feel free to replace .velocity if you want.
Answer by Bunny83 · Mar 31, 2012 at 10:27 AM
Sure a Rigidbody is a physics-object. You can use forces to move / accelerate it.
To stop all momentum just set the Rigidbody's .velocity and .angularVelocity to Vector3.zero.
@Quontex You would need to use gameobject.GetComponent().velocity = 0; and gameobject.GetComponent().angularVelocity = 0; alternatively if you use the Rigidbody in many places you could set it to a variable : rb = gameobject.GetComponent(); rb.velocity = 0; rb.angularVelocity = 0;
It is actually gameObject.GetComponent<Rigidbody2D>().velocity = 0
It was deleting some of the code due to the angle brackets to stop cross site scripting.