# Limiting gameObjects rotation on Zaxis giving wierd results

Updated on July 17, 2017 in
2 on July 16, 2017

I want to create a small Android game, where i am flying a plane, now the thing is i wanna limit the rotation of the plane , so that it wont go lesser than -45 and  greater than 45 degree in Z axis , means when ever i take joystick to right then the plane should rotate in Zaxis and stay at 45 degree and not cross 45 degree

so directly i mean to limit the Zaxis rotation of plane between -45 and 45 degree

i am using this piece of code , which is giving me pretty fair results and the gameObject is limited at 45 degree but i am having a issue that whenever the  gameobject reaches at 0 the Rotation of GameObject bounces back to max Limit provided ie, maxRotation

```     float hortemp = CrossPlatformInputManager.GetAxis("Horizontal");
float TAngle = Mathf.Clamp(hortemp, -5, 5);
transform.Rotate(0, 0, -TAngle);
float minRotation = -45f;
float maxRotation = 45f;
Vector3 currentRotation = transform.localRotation.eulerAngles;
currentRotation.z = Mathf.Clamp(currentRotation.z, minRotation, maxRotation);
transform.localRotation = Quaternion.Euler(currentRotation);
```

• Liked by
on July 16, 2017

This is most likely because Euler angles in Unity (at least in code) are always regarded as their absolute (positive) values. This means it cannot go under 0° or above 360°, and thus the -45° angle can’t be set using this way. -45° would equal 315° in Euler angles.

What’s happening once you pass the 0° threshold is that it loops back to the 360° mark and starts decreasing from there, but seeing how you clamp it with an upper value of 45°, it instantly gets locked at that after looping back.

One possible workaround for this problem could be giving the graphics of the plane a base tilt of 45° and making it clamp between 0° and 90°, which would be -45° and +45° relative to the plane itself. But I doubt this would be either efficient or convenient.

on July 17, 2017

This is most likely because Euler angles in Unity (at least in code) are always regarded as their absolute (positive) values. This means it cannot go under 0° or above 360°, and thus the -45° angle can’t be set using this way. -45° would equal 315° in Euler angles.

What’s happening once you pass the 0° threshold is that it loops back to the 360° mark and starts decreasing from there, but seeing how you clamp it with an upper value of 45°, it instantly gets locked at that after looping back.

One possible workaround for this problem could be giving the graphics of the plane a base tilt of 45° and making it clamp between 0° and 90°, which would be -45° and +45° relative to the plane itself. But I doubt this would be either efficient or convenient.

From Snowy

Thanx Alot Mate,

This trick really worked ,followed each step by you

what i did was gave the visuals an angle of -45 degree which is a child of another gameobject and let the parent gameobject stay at an angle of 45 so it may stay at 0 initially when game will start and clamped the max and min Angle value to 1 to 90 degree as giving min value as 0 is still creating that issue of bouncing back , so i gave min angle as 1 degree

```float hortemp = CrossPlatformInputManager.GetAxis("Horizontal");
float TAngle = Mathf.Clamp(hortemp, -5, 5);
transform.Rotate(0, 0, -TAngle);
float minRotation = 1f;
float maxRotation = 90f;
Vector3 currentRotation = transform.localRotation.eulerAngles;
currentRotation.z = Mathf.Clamp(currentRotation.z, minRotation, maxRotation);
transform.localRotation = Quaternion.Euler(currentRotation);
```