- Home /
 
Clamping a character's head rotation.
Hello, in my game the character's heads rotate to look at the player when they are in talking range, and do so smoothly, which works great!
 if (targetPosition && rc.characterInRange == true)
         {
 
             Quaternion rotationAngle = Quaternion.LookRotation(targetPosition.position - trackBone.position);
             trackBone.rotation = Quaternion.Slerp(trackBone.rotation, rotationAngle, Time.deltaTime * damp);
         }
         else
         {
             transform.rotation = Quaternion.Slerp(transform.rotation, originPosistion, Time.deltaTime * damp);
         }
 
               But the problem arises when the player goes too far behind them, at which point the neck will more or less crumple up, and so I was wondering- How can I clamp the rotation, preferably not allowing the head to rotate beyond -45 and 45 degrees? I've been googling this over and over but I've gotten stuck each time, unfortunately.
Pretty sure when it goes below 0 it starts and 360 and moves downwards and when above 360 it goes from 0 and upwards, so keep that in kind when clamping, you'll need to do something like this.
 public Vector3 ClampRotation(Vector3 angles, float clamp)
 {
     if (angles.x > 180)
     {
         if (angles.x < 360-clamp)
         {
             angles.x = 360-clamp;
         }
     }
     else
     {
         if (angles.x > clamp)
         {
             angles.x = clamp;
         }
     }
     if (angles.y > 180)
     {
         if (angles.y < 360-clamp)
         {
             angles.y = 360-clamp;
         }
     }
     else
     {
         if (angles.y > clamp)
         {
             angles.y = clamp;
         }
     }
     if (angles.z  > 180)
     {
         if (angles.z < 360-clamp)
         {
             angles.z = 360-clamp;
         }
     }
     else
     {
         if (angles.z > clamp)
         {
             angles.z = clamp;
         }
     }
     return angles;
 }
 
                  And to use it, you would call out after your rotation function has been performed to
 transform.localEulerAngles = ClampAngles(transform.localEulerAngles, 45.0f);
 
                 Thank you! It is close, but not quite there

It results in this... Which isn't quite what I wanted. I want to limit how much the head can really rotate on the Y axis, something like not looking over their shoulder to find you.
Answer by wackyDev · Feb 21, 2019 at 07:23 PM
Add an if statement after finding the rotation angle. If its higher then 45 then set it to 45 degrees. If its lower then -45 set it to -45. I think this is all you're looking for?
In the long run it may help to watch some character animation tutorials. Character Setup talks about restricting/clamping muscle space so a character never rotates past that given threshold (~15 minutes in).
Your answer
 
             Follow this Question
Related Questions
How to write a script to disable position and rotation tracking for VR 0 Answers
How to Clamp a Quaternion Camera Rotation? 0 Answers
Clamp Quaternions Rotation for camera 1 Answer
How to use quaternions to apply an offset to a rotation to calibrate a controller 1 Answer
Edited mouselook script rotation clamp not working 0 Answers