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;
}
}