- Home /
Stopping a rigidbody immediately
I'm very new to Unity and C#. I have a cube rigidbody that I am trying to stop once I release a movement key(WASD, etc). However, when I release the key the cube slides just a bit and that is what I am trying to prevent. I have maxed mass, velocity, material physics, and all in between with no success.
private Rigidbody rb;
private float moveHorizontal;
private float moveVertical;
public float maxSpeed = 10;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
moveHorizontal = 0f;
moveVertical = 0f;
}
// Update is called once per frame
void FixedUpdate () {
moveHorizontal = Input.GetAxis("Horizontal") * -maxSpeed;
moveVertical = Input.GetAxis("Vertical") * -maxSpeed;
rb.AddForce(moveHorizontal, 0, moveVertical, ForceMode.VelocityChange);
//Limit Speed
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = rb.velocity.normalized * maxSpeed;
}
//Stop Movement
if (Input.GetAxis("Horizontal") == 0f && Input.GetAxis("Vertical") == 0f)
{
rb.velocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
}
}
Answer by SnappleMessiah · Feb 18, 2018 at 06:58 AM
@quazeryon After some more research (and quite a few trial and errors) I tried GetAxisRaw(); and that seemed to do the trick.
Answer by quazeryon · Feb 18, 2018 at 06:33 AM
check the input options, the axis should have a "gravity" which defines how fast the values are set to 0. If you increase this value, you should be able to stop the moving body once you release the key.
Your answer
Follow this Question
Related Questions
Countering AddForce by Modifying velocity 3 Answers
Why is velocity checking intensive? 0 Answers
Rigidbody Addforce cancels out Rigidbody velocity..maybe? 0 Answers
Gravitational pull without losing speed 1 Answer
Why does writing to rigidbody.velocity after AddForce stop my rigidbody moving? 4 Answers