- Home /
Trying to Clamp x rotation for flight simulation game
Hello, everybody. I'm currently making a flight simulation game. The plane begins with an x rotation of 0, and can change depending on player input to dive or climb. I'm trying to clamp it's rotation so that it can only go 45 degrees up and 45 degrees down. I've been using clamping, and have successfully restricted it from going any further than 45 degrees down, but whenever it goes below 0 (when trying to climb, and thus the rotation becomes negative) it just snaps back to 45 degrees, facing downward. This is my script:
//Clamp values
Vector3 clampVal;
clampVal = new Vector3 (Mathf.Clamp(transform.eulerAngles.x,-45,45),transform.eulerAngles.y, transform.eulerAngles.z);
transform.eulerAngles = clampVal;
Any ideas?
I imagine Unity prefers positive rotations. -45 would actually be 315 degrees and as this is higher than 45 it snaps to 45.
Do not read from eulerAngles if at all possible. Even if as @meat5000 suggests, the angle is 315, there will come situations where the rotation will not work they way you want. I don't know how you are doing your rotation, but a better way would be to modify a Vector3 and then assign it to transform.eulerAngles. You could then do your clamping on the Vector3 before assigning it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613161513im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to Math.f Clamp this 0 Answers
Clamping Vector3 2 Answers
Rotating and orbiting an game object with a stop angle 0 Answers
Bizzare problem with limiting camera veritcal rotation 2 Answers