So, I have a script that animates the character to rotate around the y-axis based on mouse position. There is a vector directly in front of the player called reference direction in local space and a vector called look direction that varies based on mouseX in world space. The look direction rotates in world space, then the angle between the vectors is taken, and the character plays different animations to rotate at different speeds depending on the angle passed into the Horizontal aim offset parameter. The character then rotates, closing the gap between the world-space look direction and the local space reference direction, until the offset between look direction and reference direction drops below threshold. My issue is that the character only seems to turn right, because Vector3.Angle only seems to return the absolute value of the acute angle between two vectors, meaning that horizontal aim offset never becomes negative, so the character will always rotate to the right in order to reach the look direction, which is obviously not very intuitive. Here’s my code, and maybe somebody can help me to make it so that when the look direction is to the left of reference direction, the angle becomes negative:
private Animator playerAnim;
public Vector3 refDir;
public Vector3 lookDir;
public int lookSens;
public float yRotation;
public float offsetAngle;
void Start()
{
playerAnim = GetComponent<Animator>();
lookDir = new Vector3(0.0f, 0.0f, 1.0f);
}
void Update()
{
//handles look direction in relation to the reference direction to get an angle offset to be passed into the animation for idle rotation
float mouseX = Input.GetAxis("Mouse X") * Time.deltaTime * lookSens;
refDir = transform.forward;
lookDir = Quaternion.Euler(0.0f, mouseX, 0.0f) * lookDir;
offsetAngle = Vector3.Angle(refDir, lookDir);
playerAnim.SetFloat("HorAimAngle", offsetAngle);
//debug lines to track functionality
Debug.DrawRay(transform.position, refDir);
Debug.DrawRay(transform.position, lookDir);
}
thanks for any help you guys can provide