[SOLVED]Unity transform.rotation.eulerAngles doesn't work

289 Views Asked by At

[SOLVED]scroll down for answer. I'm creating a car game that controls with xbox one controller. this is steering wheel controller. my code is

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class steeringrotation : MonoBehaviour {
    void Update () {
        if (transform.rotation.eulerAngles.z == -90) {
            Debug.Log("g");
            //transform.Rotate(0,0,transform.rotation.z+25);
        }

    }
}

i changed the steering wheel z rotation to -90 and writed code: transform.rotation.eulerAngles.z == -90 and it doesn't Debug.Log("g");. what do i do? I maked -90 to 630. but again.

I Asked Google's assistant bard i copy&pasted the code and the codes doesn't work! answer: In the code i debug.log the rotation. when it was -90, debug.log says that WAS 270!!!!!!!!!. this is completed code:

if (transform.localRotation.eulerAngles.z >= 270)
    {
        Debug.Log("g");
    }
i found the transform.localRotation.eulerAngles.z in the unity discussions. first answer https://discussions.unity.com/t/how-to-get-rotation-of-an-object-using-c/166443
2

There are 2 best solutions below

3
Harsche On

Did you set z rotation on the transform from the inspector? And does the steering wheel object have a parent transform? If so, try to use transform.localEulerAngles.z instead as this is the value that actually corresponds to what you see in the inspector.

1
Mjgholizade On

Try this:

var isEqual = Mathf.Abs(transform.rotation.eulerAngles.z - (-90)) < 5f;

if(isEqual)
{
    //Do something
}