Search Unity

Resolved Euler angles are just plain out wrong???

Discussion in 'Scripting' started by jlorenzi, Feb 24, 2024.

  1. jlorenzi

    jlorenzi

    Joined:
    May 2, 2021
    Posts:
    292
    I'm trying to see if a rotation of an object loosely matches with a certain other rotation.
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Weapons : MonoBehaviour
    7. {
    8.     public Vector3 directionForRocket = new Vector3(90, 0, 90);
    9.  
    10.     private void Update()
    11.     {
    12.         Vector3 handDirection = new Vector3(RoundToTen(transform.localEulerAngles.x), 0, RoundToTen(transform.localEulerAngles.z));
    13.         Debug.Log(handDirection);
    14.         if ( handDirection == directionForRocket )
    15.         {
    16.             Debug.Log("yes");
    17.         }
    18.     }
    19.  
    20.     public int RoundToTen (float i)
    21.     {
    22.         return ((int)Math.Round(i / 10.0)) * 10;
    23.     }
    24. }
    25.  
    This doesn't work, and when looking at the inspector and console I notice that they're not the same at all.

    upload_2024-2-23_19-29-14.png

    upload_2024-2-23_19-31-55.png

    I know that in code, it doesn't use negative rotations and stuff like -90 becomes 270, but how does 90,0,90 become 90,0,0?
    I've tried local and world space euler angles and neither work. I also tried rotation.eulerAngles and that didn't work either. What am I doing wrong?
     

    Attached Files:

  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,871
    One again another reason to not use euler angles.

    The same rotation can be represented by multiple euler angles, and the euler angles you see in the inspector do not always match the actual underlying values, as the inspector is designed to be user friendly.

    Use their quaternion rotations instead. Either the
    ==
    operator, or
    Quaternion.Dot
    to test if they are relatively similar rotations to one another.
     
    CodeSmile and jlorenzi like this.