Question by
Joheil · Feb 14, 2017 at 09:59 AM ·
rotationgameobjecttriggerrotation axisgameobject.find
Check GameObject rotation in Y axis when OnTriggerEnter
Hi,
I'm trying to do "ActionA" when GameObject "B" rotation in the Y axis is 180 or -180 and enters a Trigger but it not working. My script is the following one:
void OnTriggerEnter (Collider other) {
if (GameObject.Find ("B").transform.rotation.eulerAngles.y == 180.0f) {
doActionA;
}
}
Comment
Best Answer
Answer by Hellium · Feb 14, 2017 at 10:05 AM
Because of floating imprecision, you should not compare the values with the ==
operator but with the Mathf.Approximately function for example
void OnTriggerEnter (Collider other)
{
Vector3 angles = GameObject.Find("B").transform.rotation.eulerAngles ;
Debug.Log( angles.y ) ;
if (Mathf.Approximately(angles.y, 180) || Mathf.Approximately(angles.y, -180) )
{
// doActionA;
}
}
Your answer
Follow this Question
Related Questions
how to point the positive Y axis of an object to a direction 1 Answer
Unity 2019 can't change/access GameObject Variables from another script 0 Answers
Rotate to a specific angle on one axis 1 Answer
How do I rotate my object back to the world's up direction whilst keeping it's forward direction? 0 Answers