- Home /
Using AddTorque to rotate but can't stop it
New to unity, so this might be very simple (hopefully).
I'm using AddForce to move a body along, at a continuous speed, that's no issue...
rigidBody.AddForce(transform.forward Time.deltaTime forceApplied, ForceMode.VelocityChange);
Then I have simple code to turn the object, using AddTorque
if (Input.GetKey(KeyCode.D))
{
rigidBody.AddTorque(transform.up * forceApplied/5 * Time.deltaTime, ForceMode.VelocityChange);
}
if (Input.GetKey(KeyCode.A))
{
rigidBody.AddTorque(transform.up * -forceApplied/5 * Time.deltaTime, ForceMode.VelocityChange);
}
but when I stop pressing the A or D keys, the object continuous to move around in a circle.
What I want is very basic "steering", so that if you hold down A/D, you turn in the appropriet direction but once you release the key, you resume straight line movement.
Note for this exercise, I'm not worries about Mass being involved (that's why I'm using Velocity Change).
Thanks for any advice!
Answer by BastianUrbach · Feb 14 at 09:58 AM
To stop an object from moving or rotating, you would have to apply a force or torque to it.
"A body continues in its state of rest, or in uniform motion in a straight line, unless acted upon by a force." - Isaac Newton
You'll have to decide if you want realistic, physically based movement or unrealistic, "snappy" controls. If you want realistic movement, you can set a higher drag and angular drag on the rigidbody. These will slow down the rigidbody in a manner that approximates air resistance. If the rigidbody lies on a surface, you can also increase its friction or that of the surface by assigning an appropriate Physics Material to the Collider. If you just want the object to move as you say without much regard for physical accuracy, you can set the velocity and angularVelocity of the rigidbody directly instead of applying forces and torques. To stop it, you just set them to zero.
Side note: Instead of multiplying by delta time manually, you can also use ForceMode.Acceleration. That still ignores mass but does the multiplication by Time.deltaTime for you.
Answer by xavram · Feb 14 at 03:49 PM
Thank you for the tips, particularly the part about Acceleration vs VelocityChange!
Your answer
