- Home /
Get an eular angle between 0 and 360 after calculations
Summary of my issue
Hello, I believe I am making a mistake. What I am trying to do is limit a rotation of an object in either two directions. Here is some code: if (startTower.transform.eulerAngles.y > 90 && startTower.transform.eulerAngles.y < 270)
{
Debug.Log(startTower.transform.eulerAngles.y + 90);
}
else if (startTower.transform.eulerAngles.y < 60 || startTower.transform.eulerAngles.y > 300)
{
Debug.Log(startTower.transform.eulerAngles.y + 90);
}
The issue
Now the issue with the above, is that at 360, it adds 90. so it's 450 and I want 450-360, = 90. So I want 90. So is there a way to convert 450 into 90 while doing the calculation?
Or more specifically, is there a way to always get the degrees between 0 and 360, even if I add 90?
Note: You don't need to do this, I'll accept the answer if the above is right. So, if you are feeling generous, from the picture below, I want the wall to only appear on those two sides, where the blue arrows are. So left of the wall, or right of the wall.
So my plan is to replace the debug.log, with Mathf.Clamp, and give it 60 degrees leeway. If you would like to give a better solution than my idea, I am willing to try it.
End of generosity
Any help would be greatly appreciated
Answer by haruna9x · Oct 29, 2017 at 08:51 AM
Try it:
public static float Clamp0360(float eulerAngles)
{
float result = eulerAngles - Mathf.CeilToInt(eulerAngles / 360f) * 360f;
if (result < 0)
{
result += 360f;
}
return result;
}
Answer by unity_dS85HyjyiWEwcQ · Mar 28 at 09:02 AM
It would be quicker and more concise to just use the remainder operator.
eulerAngles %= 360;
Your answer
Follow this Question
Related Questions
Find the difference between the Z rotation of two GameObjects 2 Answers
Mathf.SmoothDamp freeking out? 0 Answers
Clamp Rotation Problem 1 Answer
Rotate object towards joystick input using c# 1 Answer
Rotation Limit 2 Answers