I'm working on a simple bit of code in C# to make sure a player model's head points at the mouse's Vector3 position (lookPoint), but that it clamps between a 90 degree range, 45 degrees to either side of the torso's current direction.
I've played around with the results of euler angles to make sure I'm getting the desired rotation value for the y axis, but I struggle with when the euler angle should cycle over to 0 again, and I can't seem to figure out how to sort it out.
minRot = myTorso.transform.rotation.eulerAngles.y-180f-45f;
maxRot = myTorso.transform.rotation.eulerAngles.y-180f+45f;
lookDirection = Mathf.Atan2(lookPoint.x - transform.position.x, lookPoint.z - transform.position.z);
lookRotation = Mathf.Clamp(Mathf.Rad2Deg * lookDirection, minRot, maxRot);
myHead.eulerAngles = new Vector3(0,lookRotation,0);
This is causing the head to snap back to one of the extremes when it cannot figure out what it's max or min should be.
Can anyone help me to define the minRot and maxRot so that it accounts for the 180 degree crossover?