Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 toishaanpatel · Jun 24, 2020 at 02:54 AM · rotationquaternionplayer movementlookatlateupdate

How to make player look at opponents?

I want to make the player's head look at a nearby opponent if the opponent drives past the player. My confusion is how I would do this through an animation. I tried using lateUpdate and it mostly worked, but since blender uses a different rotational axis with z being up down, while in Unity, y is up and down, I get a really weird rotation of the player's head. How can I translate this into a proper rotation facing the opponent on only one axis? Thanks

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 UnityedWeStand · Jun 24, 2020 at 03:37 AM

I assume the weirdness is coming from the fact that you need to rotate the head in unity to get it "right side up" after exporting from blender. This causes the object to have a starting non-zero rotation, which makes any further changes to the rotation XYZ values non-intuitive.

The Easy Way


For all objects imported from blender, do not use them directly in Unity. Instead, set them as a child of an empty GameObject. You can rotate the child so that it is "right side up" in Unity, then perform all subsequent rotation stuff on the parent, which starts off with zeroed out rotation.


The Quaternion Way


Quaternions will help you immensely in this situation. First, with the object at zeroed out rotation, determine which of the object's transformation axes is the head's forward direction. Let's pretend that the imported head at zero rotation has the front directed upward along the Y-axis.


After rotating the imported object to be right side up, use Quaternion.FromToRotation() to create the "final state" rotation with the head looking at the target.

 Quaternion finalRotation = Quaternion.FromToRotation(head.transform.up, new Vector3(target.transform.x - head.transform.x, 0, target.transform.z - head.transform.z))

The y component of the "To" Vector3 is set to zero to ensure rotation purely along the y-axis.

Finally, use Quaternion.Slerp() to animate the rotation via the time parameter. Note that you will need to store the initial rotation of the head for this method.

 Quaternion initialRotation = head.transform.rotation;
 
 void Update()
 {
     head.transform.rotation = Quaternion.Slerp(Quaternion.identity, finalRotation, time) * initialRotation;
     time += Time.deltaTime;
 }


Voila.

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

176 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 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

Align Player to Regional Up Direction, Without Altering Look Direction Dramatically 0 Answers

Copying the rotation matrix of an object. 3 Answers

Smoothly rotate a bone around X axis in LateUpdate() 1 Answer

Rotating object to face target, while being rotated to match terrain slope 1 Answer

LookAt on only 1 axis for 2D games? 1 Answer


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