- Home /
Question by
Brachiosaurus · Jan 02, 2018 at 04:45 PM ·
rotationphysics2deuleranglesclampclamped rotation
Limit Rotation of physics object in 2D
All I'm trying to do is limit a gameobject's z rotation between two numbers. I did a whole bunch of research on how to use Mathf.Clamp, and if I understand it correctly, I don't see why this wouldn't work:
void FixedUpdate () {
this.transform.rotation = Quaternion.Euler(transform.eulerAngles.x, transform.eulerAngles.y, Mathf.Clamp(transform.eulerAngles.z, -25.0F, 25.0F));
}
is there something I'm missing? In the game, the object randomly jolts and twitches and I don't know why...
Comment
Best Answer
Answer by BastianUrbach · Jan 02, 2018 at 06:16 PM
The values in transform.eulerAngles appear to be in the range 0 to 360, not -180 to 180. Limiting the rotation to the range -25 to 25 won't work because of that. Try this instead:
Vector3 euler = transform.eulerAngles; if (euler.z > 180) euler.z = euler.z - 360; euler.z = Mathf.Clamp(euler.z, -25, 25); transform.eulerAngles = euler;Note also that you are currently not changing the angularVelocity of the rigidbody. For the physics engine it will still appear to rotate, so you might wnat to set that to zero (just for the z-Axis) whenever the rotation hits the limits.
Thank you so much, this works like a charm! You have saved my sanity!
Thank you! I've been looking for this kind of solution all over! Do you know how difficult it is to find this answer??? Thank you!