Reading Quaternion.eulerAngles.y smaller 45 and greater 315 does not work
Reading Quaternion.eulerAngles.y smaller 45 and greater 315 does not work
Hi, I have got a player that always faces the mouse and can move in all directions (Top Down View, 3D). This way you can for example move bakcwards but still look in any direction. To animate the player correctly, animations should be called depending on Input and on the Y-axis rotation of the player (see script). For that I have divided the the 360 degrees in 4 parts. While the last 3 if statements work just fine, the first one, which should read y-angles smaller 45 and greater 315 does not work. (the Quaternions are saved in the static variable PlayerPosition.playerRot). I tried different values and bools and was thinking about trying the mouseposition input, but there angles would be needed too.
Does anyone have an idea why? Any help and suggestions are highly appreciated
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerWhiteAnimation : MonoBehaviour
{
Animator anim;
void Start ()
{
anim = GetComponent<Animator>();
}
void Update ()
{
// this first if statement does not work...
if (PlayerPosition.playerRot.eulerAngles.y >= 315 && PlayerPosition.playerRot.eulerAngles.y <= 45)
{
if (Input.GetKey(KeyCode.A)) anim.SetTrigger("MovesLeft");
if (Input.GetKey(KeyCode.D)) anim.SetTrigger("MovesRight");
if (Input.GetKey(KeyCode.S)) anim.SetTrigger("MovesBack");
if (Input.GetKey(KeyCode.W)) anim.SetTrigger("MovesForw");
}
else
if (PlayerPosition.playerRot.eulerAngles.y >= 45 && PlayerPosition.playerRot.eulerAngles.y <= 135)
{
if (Input.GetKey(KeyCode.W)) anim.SetTrigger("MovesLeft");
if (Input.GetKey(KeyCode.S)) anim.SetTrigger("MovesRight");
if (Input.GetKey(KeyCode.D)) anim.SetTrigger("MovesForw");
if (Input.GetKey(KeyCode.A)) anim.SetTrigger("MovesBack");
}
else
if (PlayerPosition.playerRot.eulerAngles.y >= 135 && PlayerPosition.playerRot.eulerAngles.y <= 225)
{
if (Input.GetKey(KeyCode.W)) anim.SetTrigger("MovesBack");
if (Input.GetKey(KeyCode.S)) anim.SetTrigger("MovesForw");
if (Input.GetKey(KeyCode.D)) anim.SetTrigger("MovesLeft");
if (Input.GetKey(KeyCode.A)) anim.SetTrigger("MovesRight");
}
else
if (PlayerPosition.playerRot.eulerAngles.y <= 315 && PlayerPosition.playerRot.eulerAngles.y >= 225)
{
if (Input.GetKey(KeyCode.W)) anim.SetTrigger("MovesRight");
if (Input.GetKey(KeyCode.S)) anim.SetTrigger("MovesLeft");
if (Input.GetKey(KeyCode.D)) anim.SetTrigger("MovesBack");
if (Input.GetKey(KeyCode.A)) anim.SetTrigger("MovesForw");
}
}
}
sloved it, and sorry I found the solution in the forum here https://answers.unity.com/questions/467139/identify-range-of-rotation-eulerangles-not-working.html
in this case, ins$$anonymous$$d of using Quaternions it is better to use Vector3.Angle(transform.foward, Vector3.forward) < 45. When looking north, the <45 gives the range between 315 and 45.<90 would give 270 to 90. Also, there was no need to store the Quaternions in a static variable, I could have just refered to the parent. See working script below
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerBlackAnimation : $$anonymous$$onoBehaviour
{
Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
if (Vector3.Angle(transform.parent.forward, Vector3.forward)< 45)
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) anim.SetTrigger("$$anonymous$$ovesLeftBl");
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) anim.SetTrigger("$$anonymous$$ovesRightBl");
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S)) anim.SetTrigger("$$anonymous$$ovesBackBl");
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)) anim.SetTrigger("$$anonymous$$ovesForwBl");
}
else
if(Vector3.Angle(transform.parent.forward, Vector3.right) < 45)
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)) anim.SetTrigger("$$anonymous$$ovesLeftBl");
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S)) anim.SetTrigger("$$anonymous$$ovesRightBl");
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) anim.SetTrigger("$$anonymous$$ovesForwBl");
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) anim.SetTrigger("$$anonymous$$ovesBackBl");
}
else
if (Vector3.Angle(transform.parent.forward, -Vector3.forward) < 45)
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)) anim.SetTrigger("$$anonymous$$ovesBackBl");
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S)) anim.SetTrigger("$$anonymous$$ovesForwBl");
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) anim.SetTrigger("$$anonymous$$ovesLeftBl");
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) anim.SetTrigger("$$anonymous$$ovesRightBl");
}
else
if (Vector3.Angle(transform.parent.forward, -Vector3.right) < 45)
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)) anim.SetTrigger("$$anonymous$$ovesRightBl");
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S)) anim.SetTrigger("$$anonymous$$ovesLeftBl");
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) anim.SetTrigger("$$anonymous$$ovesBackBl");
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) anim.SetTrigger("$$anonymous$$ovesForwBl");
}
}
}
Not sure though how to get an diagonal angle between for example 300 and 330.
Any advice?
Your answer
![](https://koobas.hobune.stream/wayback/20220612164534im_/https://answers.unity.com/themes/thub/images/avi.jpg)