• 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
1
Question by superventure · Mar 27, 2016 at 10:04 PM · rotationlimitboneheaddegree

Limit transform.LookAt to 90 degrees based off of object's forward axis

Please do not refer me other scripts or asset store packages, mecanim or otherwise, thank you.

I am now at a point in my game where I would like to have my characters heads look directly at other points.

I have a lot of characters rigged with bones not ideally oriented to have correct rotations, ie: Head /neck bones with the y axis backwards or flipped, or something weird. When I first started modelling, I was not aware of the importance of setting all bones axis' orientations.

Anyways, It's simply not ideal for me to go and re bone, and re skin, and re animate everyone from scratch.

I have accomplished the rotation fix by using space self and transform.Look At

 var pref :  Vector3; 
 var point : Transform;
 
 
 function LateUpdate () {
 
  transform.LookAt(point.position);
 transform.Rotate(pref.x, pref.y, pref.z, Space.Self);
 }

This works beautifully, but of course is unnatural for a head/neck bone. Imagine when the point an npc is looking at walks behinds them. The head will rotate too much.

This is what I need help with. How can I

Limit the rotation of a bone to not exceed a certain degree beyond the root character's forward axis?

for example, when the point the npc is looking at goes over 90 degrees relative of the character's forward axis (not the head/neck bone, because it's axis is weird to start with ), make the head/neck bone STOP where it's at.

I'm simply trying to not allow full 360 head rotation based of the forward of the initial npc's object.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LazyElephant · Mar 27, 2016 at 10:52 PM

Use Vector3.Angle()to calculate the angle between your character's forward vector and the point you're looking at. If the angle is greater than 90 degrees, only rotate your head by 90 degrees, otherwise rotate it to the appropriate angle.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

47 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

Related Questions

Problem With 360 Rotation Degrees 2 Answers

Limiting RotateTowards on two axes 1 Answer

Limit local rotation around x, y, and z axes? 0 Answers

Limit Rotation of Rigidbody to 45 Degrees (Handlebars on a Bicycle) 1 Answer

How to make a bone rotate facing towards a given Transform? 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