0

[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 Answers2

0

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.

Harsche
  • 31
  • 1
  • 1- yes 2-yes. let me try transform.localEulerAngles.z – NoobGangNB1234 Jun 14 '23 at 08:16
  • but it gives me error: error CS1061: Type `UnityEngine.Quaternion' does not contain a definition for `localEulerAngles' and no extension method `localEulerAngles' of type `UnityEngine.Quaternion' could be found. Are you missing an assembly reference? – NoobGangNB1234 Jun 14 '23 at 08:20
  • Are you sure you're using `transform.localEulerAngles.z` and not `transform.localRotation.localEulerAngles`? The error suggests you're trying to get the euler angles from a quaternion when you should be accessing it from a transform. – Harsche Jun 14 '23 at 16:07
0

Try this:

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

if(isEqual)
{
    //Do something
}