float directionFacingX;
float directionFacingY;
float directionFacingZ;
void update:
directionFacingX = transform.rotation.eulerAngles.x;
directionFacingY = transform.rotation.eulerAngles.y;
directionFacingZ = transform.rotation.eulerAngles.z;
float directionFacingX;
float directionFacingY;
float directionFacingZ;
void update:
directionFacingX = transform.rotation.eulerAngles.x;
directionFacingY = transform.rotation.eulerAngles.y;
directionFacingZ = transform.rotation.eulerAngles.z;
What do you mean “detect?” One approach is to see which of the 3 axes has the largest absolute value compared to the others, and consider that the dominant axis-aligned direction.
Also, you can’t really reliably read .eulerAngles from Quaternions. The API allows you to do it but it’s not reliable, as Quaternions do not store Euler angles internally.
I’m trying to make something happen when it detects my player facing a direction.
That’s an unusual way to state the problem.
Generally you will have an expression that evaluates the input dataset (perhaps the current input of the player, or the velocity of the player?) to true or false, and you use an if statement to test that, and to conditionally do the things you want based on that expression.
For instance, if I have horizontal movement, in Unity traditionally if it is 0 it is not moving, if it is less than zero it is moving left, if it greater than zero it is moving right:
float horizontalMovement = ... (acquired however you acquire it)
if (horizontalMovement > 0)
{
Debug.Log( "Moving right.");
}
etc.
EulerAngles is not going to contain the information you want. (Euler angles are almost never the right answer in general, but especially not here) What you really want to do is compare your forward vector to a given direction vector, which you can do like this:
Vector3 desiredVector = Vector3.right;
Vector3 characterVector = transform.forward;
float angleBetween = Vector3.Angle(characterVector, desiredVector);
if (angleBetween < 45f) {
Debug.Log("Transform is facing rightward");
}