Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KenanTheFab · Feb 21, 2019 at 06:43 PM · rotationquaternionclamptracking

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · Feb 21, 2019 at 07:19 PM 0
Share

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);
avatar image KenanTheFab RobAnthem · Feb 21, 2019 at 11:36 PM 0
Share

Thank you! It is close, but not quite there

alt text

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

135 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges