- Home /
is there any way to get a sane number on an objects current y rotation?
Want to build a clock, rotate a plane, how can I see when the plane is rotated 0, 90, 180, 270 degrees etc? (I would like to read these rotation states then do other actions and so on)
All I get is a snapping from 90 to 270 in the readout. Or worse.
Is there any sane way to do this without descending into madness?
Do you need to know when it is at 0,90,180 and 270 or when it was rotated from its initial rotation by 0,90,180,270...?
Answer by GuyTidhar · Apr 25, 2012 at 10:17 AM
I think this should do the trick:
var previousSection : int;
var baseY : float;
var currentY : float;
function Awake()
{
baseY = transform.eulerAngles.y;
previousSection = Mathf.RoundToInt(transform.eulerAngles.y - baseY) / 90;
}
function Update()
{
currentSection = Mathf.RoundToInt(transform.eulerAngles.y - baseY) / 90;
if ( currentSection != previousSection )
{
previousSection = currentSection;
Debug.Log("New Section! = " + currentSection);
}
}
You should round the result of the division, not eulerAngles.y:
$$anonymous$$athf.RoundToInt((transform.eulerAngles.y - baseY) / 90)
This will center the values around 0, 90, 180 and 270, giving about +/- 45 degrees margin around each angle.
Answer by hawken-2 · Apr 25, 2012 at 10:26 AM
not working for me entirely, seems to dump 0 & 3 then 2 & 1 at the same time in the debug. I added the following:
var previousSection : int;
**var currentSection : int;**
var baseY : float;
var currentY : float;
function Awake()
{
baseY = transform.eulerAngles.y;
previousSection = Mathf.RoundToInt(transform.eulerAngles.y - baseY) / 90;
}
function Update()
{
**transform.Rotate(0, -1, 0);**
currentSection = Mathf.RoundToInt(transform.eulerAngles.y - baseY) / 90;
if ( currentSection != previousSection )
{
previousSection = currentSection;
Debug.Log("New Section! = " + currentSection);
}
}
Your answer

Follow this Question
Related Questions
Ball Rolling Animation Not Working Properly 0 Answers
Fixing the rotation around a particular axis using Quaternions? 2 Answers
Circling around a target while maintaining direction of facing 0 Answers
Rotating Camera Around Central Point Using Arrow Keys 1 Answer
Convert Quaternion to Eulerangles 2 Answers