Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 yautja-dev · Mar 30, 2020 at 08:50 PM · rotationquaternionlateupdate

Smoothly rotate a bone around X axis in LateUpdate()

I would like to activate the shoulder canon of my model by rotating it around it's local X axis 180 degrees. The model is in an idle animation state when "2" is pressed so I've read that the bone needs to be transformed in LateUpdate(). I've also read that Lerp() is necessary to achieve a smooth/natural rotation. Here is my code:

 public class WarriorController : MonoBehaviour
 {
     private bool shooting;
     private Transform gunBone;
     private float _startRotateTime;
     private Quaternion _startRotation;
     private Quaternion _endRotaion;
         
     void Start()
     {
         gunBone = GameObject.Find("gun").transform;
     }
     
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Alpha2))
         {
             shooting = true;
             _startRotateTime = Time.time;        
             _startRotation = gunBone.rotation;
             _endRotaion = Quaternion.Euler(gunBone.localEulerAngles.x + 180, gunBone.localEulerAngles.y, gunBone.localEulerAngles.z);
          }
     }
         
     void LateUpdate()
     {
         if (shooting == true)
         {
             float timeSinceStarted = Time.time - _startRotateTime;
             float percetageComplete = timeSinceStarted / 1f;
             gunBone.rotation = Quaternion.Lerp(_startRotation, _endRotaion, percetageComplete);
             //gunBone.localEulerAngles = new Vector3(gunBone.localEulerAngles.x, 0, 0);
         }        
     }
 }
 

The rotation rotates smoothly but seems to be rotating on the Z axis and not a full 180?

Here is the beginning of the rotation: alt text

Here is the end of the rotation: alt text

I believe I'm failing to understand how to work with Quaternions. Please advise! Thank you!

startrotate.png (92.5 kB)
endrotate.png (103.0 kB)
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 yautja-dev · Apr 02, 2020 at 07:23 PM

All I really needed was the Transform.LocalRotation......

This is what is working for me:

 using UnityEngine;
 
 namespace GunController
 {
     public class GunController : MonoBehaviour
     {
         private bool _isLerping = false;
         private bool shooting = false;
         private float _startRotateTime;
         private Transform _gunBone;
         Quaternion _gunBoneHomeRotation;
         private Quaternion _gunBoneStartRotation;
         private Quaternion _gunBoneEndRotaion;
         
         private void Start()
         {
             _gunBone = GameObject.Find("gun").transform;
             _gunBoneHomeRotation = _gunBone.localRotation;                
         }
 
         private void Update()
         {
             if (Input.GetKeyDown(KeyCode.Alpha2))
             {
                 if (shooting == false && !_isLerping)
                 {
                     shooting = true;
                     _isLerping = true;
                     _startRotateTime = Time.time;
 
                     _gunBoneStartRotation = _gunBone.localRotation;
                     
                     float toAngle;
                     Vector3 toVector;
                     var toAngleAxis = _gunBoneStartRotation * Quaternion.Euler(180, 0, 0);
                     toAngleAxis.ToAngleAxis(out toAngle, out toVector);
 
                     _gunBoneEndRotaion = Quaternion.AngleAxis(toAngle, toVector);
                 }
                 if (shooting == true && !_isLerping)
                 {
                     shooting = false;
                     _isLerping = true;
                     _startRotateTime = Time.time;
                     _gunBoneStartRotation = _gunBone.localRotation;
                     _gunBoneEndRotaion = _gunBoneHomeRotation;
                 }
             }
         }
 
         private void LateUpdate()
         {
             if (_startRotateTime <= Mathf.Epsilon) { return; } // Protect against NAN
             if (shooting)
             {
                 RotateGunBone(_gunBone, _gunBoneStartRotation, _gunBoneEndRotaion, _startRotateTime);
             }
             if (!shooting)
             {
                 RotateGunBone(_gunBone, _gunBoneStartRotation, _gunBoneEndRotaion, _startRotateTime);
             }
         }
 
         void RotateGunBone(Transform bone, Quaternion from, Quaternion to, float startTime)
         {
             float timeSinceStarted = Time.time - startTime;
             float percetageComplete = timeSinceStarted / .5f;
             bone.localRotation = Quaternion.Lerp(from, to, percetageComplete);
             if (percetageComplete > 1.0f)
             {
                 _isLerping = false;
             }
         }
     }
 }    

This is an amazing tutorial on Quaternions!

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

162 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

Related Questions

Rotation problem while dynamically changing animations 2 Answers

How to make player look at opponents? 1 Answer

How can i sync the rotation of players (clients) on a dedicated server? 0 Answers

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

Rotation to go from one direction to another? 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