When adding torque you can’t limit the spin speed. Rigidbody2D doesn’t have a version of maxAngularVelocity like Rigidbody3D has.
What’s the best way to do this?
When adding torque you can’t limit the spin speed. Rigidbody2D doesn’t have a version of maxAngularVelocity like Rigidbody3D has.
What’s the best way to do this?
You could always cap the angular velocity on the Rigidbody2D yourself. Something simple like this should do it:
void FixedUpdate()
{
if(rb.angularVelocity > maxAngularVelocity ) {
rb.angularVelocity = maxAngularVelocity;
}
}
Thanks. That didn’t work properly at first, but I just I looked at it with Debug.Log and saw that it went into the negative when going right, so I just had to add another condition to your example.
void ClampAngularVelocity()
{
if (rb.angularVelocity < -maxAngularVelocity) { rb.angularVelocity = -maxAngularVelocity; }
if (rb.angularVelocity > maxAngularVelocity) { rb.angularVelocity = maxAngularVelocity; }
}
I used this because angularVelocity is a Vector3:
float fVertSpeed = vertical * Speed * fixedDeltaTime;
float fHorSpeed = horizontal * Speed * fixedDeltaTime;
rb.AddRelativeTorque(new Vector3(fVertSpeed, 0, fHorSpeed), ForceMode.VelocityChange);
Vector3 angularVel = rb.angularVelocity;
if (angularVel.x < -maxAngularVelocity;) { angularVel.x = -maxAngularVelocity; }
if (angularVel.x > maxAngularVelocity;) { angularVel.x = maxAngularVelocity; }
if (angularVel.y < -maxAngularVelocity;) { angularVel.y = -maxAngularVelocity; }
if (angularVel.y > maxAngularVelocity;) { angularVel.y = maxAngularVelocity; }
if (angularVel.z < -maxAngularVelocity;) { angularVel.z = -maxAngularVelocity; }
if (angularVel.z > maxAngularVelocity;) { angularVel.z = maxAngularVelocity; }
rb.angularVelocity = angularVel;
Please don’t necro posts like this, from 2016!
The above post is talking about 2D, angularVelocity is a scalar. You’re referring to 3D physics.