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 Baalhug · Jul 22, 2019 at 08:28 PM · rotationmodelanimated

To rotate a head following a target

Hi, guys:

I've been hours trying to accomplish something easy (or it looked like). I have a character model with an animation imorted into unity. It works good. Now I want the head looks in the direction of a target, even if the model is being animated. The animations make the character turns left or right by 60 degrees. So I wrote this code:

 using UnityEngine;
 
 public class CharBehaviour : MonoBehaviour
 {
 
     public Transform head;
     public Transform torax;
     public Transform target;
     private Animator anim;
 
     private void Start()
     {
         anim = GetComponent<Animator>();
     }
 
     void LateUpdate()
     {
         Quaternion lookRotation = Quaternion.LookRotation(target.position - head.position);
         if (lookRotation.eulerAngles.y > 60)
         {
             anim.Play("Turn_right");
         }
         if (lookRotation.eulerAngles.y < -60)
         {
             anim.Play("Turn_left");
         }
         head.rotation = lookRotation * head.rotation;
     }
 }

It works nice without the animation, the head indeed follows the target. But when I play the animation (lines 19-26), the object torax (grandparent of head) gets rotated by 60 degrees and the head is turned 60 degrees from the target direction (which is expected). I tried a lot of combinations after last line but none works. The head gets rotated in odd ways. If I do:

 head.rotation = torax.rotation * head.rotation;

It does not work. Using localRotation no matter where it doesn't work either. I'm aware I don't understand Quaternions intuitivelly, but afaik combining rotations is done by multipliying them. What am I missing? Any ideas?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Baalhug · Jul 23, 2019 at 02:38 PM

Ok, I found the problem and the solution. The problem was character bones were imported into unity in different axis. Model was ok, but bones were not. So the solution is: In the FBX export menu in Blender there is a pair of options: (Primary and Secondary Bone Axis). I set them up to Primary = Z axis and Secondary = X axis. That solved the problem of bones. Then I just changed the lookRotation stuff for head.lookAt(target). Now it works perfect.

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
avatar image
0

Answer by JonPQ · Jul 22, 2019 at 10:50 PM

point the head using code, not animation. can't you do head.rotation = lookRotation to make the head look at the target ? If this works, add in a blended lerp, so the head has some acceleration and deceleration.

Comment
Add comment · Show 1 · 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
avatar image Baalhug · Jul 22, 2019 at 11:37 PM 0
Share

The head is already rotated using code, but the animation moves the whole character, so it rotates the head too.

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

146 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

Related Questions

Rotation Issue 1 Answer

Rotating a 3D object relative to it's velocity, but only on a 2D plane 0 Answers

Model deforming after rotation. 0 Answers

cant freeze rotation or set it manually to 0 on Zombie Model, Freezing or setting rotation not working 1 Answer

3D Character Models 2 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