- Home /
Clamping look rotation 3d?
So, I'm making a third person game right now, and when the character is standing still he is supposed to rotate with the mouse. There is a vector in front of him that is for reference then a desired look vector that moves proportional to the mouse's x value. then the angle between these two is gotten and that value is passed into a blend tree to play the animation at the appropriate speed. The issue is that this desired look direction vector can just go in a circle, so that if the player keeps turning the mouse, the desired look direction goes past halfway and registers as turning the opposite of the intended way, because I'm not sure how to clamp the angle that the desired look vector will rotate to. I tried clamping the mouseX value and that didn't seem to help, and I'm not sure what other places I can place the clamp. Thanks in advance, and hopefully you can kind of visualize what I mean. Here's the code:
private Animator playerAnim;
public Vector3 refDir;
public Vector3 lookDir;
public Vector3 turnDir;
public float turnAngle;
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;
turnDir = transform.right;
lookDir = Quaternion.Euler(0.0f, mouseX, 0.0f) * lookDir;
offsetAngle = Vector3.Angle(refDir, lookDir);
turnAngle = Vector3.Angle(turnDir, lookDir);
if (turnAngle > 90)
{
offsetAngle *= -1;
}
else if (turnAngle < 90)
{
}
playerAnim.SetFloat("HorAimAngle", offsetAngle);