Benjamin Venema
Benjamin Venema

Reputation: 25

Seeing if rotation has chaned with if statement

I am trying to see if my prefabs has chanced rotation in anyway but this ain't working any one know how i have to do this I am calling this script in other script and that's working the Debug.Log Shows up

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

public class Pawn : MonoBehaviour
{
    private GameObject pawnName;

    public Transform tf;


    [SerializeField]
    float eulerAngX;
    [SerializeField]
    float eulerAngY;
    [SerializeField]
    float eulerAngZ;



    public void Start()
    {
        tf = GetComponent<Transform>();
    }

    public void OnTriggerEnter(Collider other)
    {
        
    }

    public void PawnHasFallen()
    {
        eulerAngX = transform.localEulerAngles.x;
        eulerAngY = transform.localEulerAngles.y;
        eulerAngZ = transform.localEulerAngles.z;
        Debug.Log("Calling this script");

        if (eulerAngX < 0 || eulerAngX > 0 || eulerAngY < 0 && eulerAngY > 0 || eulerAngZ > 0 || eulerAngZ < 0)
        {
            Debug.Log(pawnName + "Has Fallen");
        }
       
    }

}

Upvotes: 0

Views: 88

Answers (1)

derHugo
derHugo

Reputation: 90580

In general note that due to floating point precision general checks for exact equality might fail and you should rather either use a certain threshold or Mathf.Approximately.

So instead of

eulerAngX < 0 || eulerAngX > 0

you might rather want to use

!Mathf.Approximately(eulerAngX, 0)

or even simplier in order to check if any of them is not 0 at once

if(!Mathf.Approximately(transform.localEulerAngles.sqrMagnitude, 0))

Note though that for a pawn which can be rotated around Y without fall it is way simplier to check if it's UP vector is still pointing UP ;)

if(!Mathf.Approximately(Vector3.Angle(Vector3.up, transfom.up), 0f))
{
    Debug.Log($"{name} has Fallen", this);
}

Upvotes: 1

Related Questions