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 Matt_Lesliee · Jan 27 at 11:13 PM · rotationquaternionrotation detection

Replicating the rotation of one game object on another game object using Quaternions.

Good day everybody. I have been bashing my head over this problem for a while now. I'm trying to create a script that can be applied to a game object. The purpose of the script is to take another game objects transform in and replicate the difference in rotations of that transform on the game object the script is attached too.

The script needs to be able to follow each independent axis rotation solely, or two of axes, or even all three. The code below works if all three of the axes are monitored, when the x and z axis are solely monitored, when the y and z axis are solely monitored, but NOT when the x and y axis are solely monitored (The game object that's following (with the script attached)) generates a value for the z axis when trying to rotate just the x and y axis.

It becomes clear that whenever I'm trying to replicate the rotation of two of the axes and the z axis is not one of those axes I'm running into issues. I have done this with quaternions to avoid gimbal lock. I do believe there is an issue with the implementation of my w axis when creating the Quaternion (Which represents the rotation I want to apply to the Game object).

If anyone may provide some insight I'd be very grateful, I am a junior developer and have only started working with unity this year.

Please find the code below

 [ExecuteInEditMode]
 public class RotationSync2 : MonoBehaviour
 {
 
     [SerializeField]
     public Transform ExternalTransform;
 
     private Quaternion PreviousExternalTransform;
     private Quaternion RotationDifference;
     private Quaternion Temp = new Quaternion();
     private Quaternion PreviousTemp;
 
     private float xToUse;
     private float yToUse;
     private float zToUse;
 
     private bool RotationComplete = false;
    
     [HideInInspector] public bool rotateX;
     [HideInInspector] public bool rotateY;
     [HideInInspector] public bool rotateZ;
 
     [HideInInspector] [InspectorName("Invert")] public bool InvertX;
     [HideInInspector] [InspectorName("Invert")] public bool InvertY;
     [HideInInspector] [InspectorName("Invert")] public bool InvertZ;
 
     [HideInInspector] public float percentageOfRotationToFollowX = 1f;
     [HideInInspector] public float percentageOfRotationToFollowY = 1f;
     [HideInInspector] public float percentageOfRotationToFollowZ = 1f;
 
     private float rotationToSetX;
     private float rotationToSetY;
     private float rotationToSetZ;
 
     // Start is called before the first frame update
     void Start()
     {
         PreviousExternalTransform = ExternalTransform.rotation;
     }
 
     // Update is called once per frame
     void Update()
     {
         ClearPreviousRotationValues();
         BuildQuarternion();
         FindRotateDifference();
         SetRotationValues();
         SetRotations();
     }
 
     public void ClearPreviousRotationValues()
     {
         rotationToSetX = 0;
         rotationToSetY = 0;
         rotationToSetZ = 0;
     }
 
     private void BuildQuarternion()
     {
         if (ExternalTransform.rotation.x == PreviousExternalTransform.x && rotateX)
         {
             Temp.x = 0f;
         }
         else
         {
             Temp.x = ExternalTransform.rotation.x;
         }
 
         if (ExternalTransform.rotation.y == PreviousExternalTransform.y && rotateY)
         {
             Temp.y = 0f;
         }
         else
         {
             Temp.y = ExternalTransform.rotation.y;
         }
         if (ExternalTransform.rotation.z == PreviousExternalTransform.z && rotateZ)
         {
             Temp.z = 0f;
         }
 
         else
         {
             Temp.z = ExternalTransform.rotation.z;
         }
         Temp.w = ExternalTransform.rotation.w;
 
     }
 
     private void FindRotateDifference()
     {
         Quaternion a = Quaternion.identity * Quaternion.Inverse(Temp);
         Quaternion b = Quaternion.identity * Quaternion.Inverse(PreviousTemp);
 
 
         if (ExternalTransform.rotation != PreviousExternalTransform && PreviousTemp != null)
         {
             RotationDifference = b * Quaternion.Inverse(a);
             RotationComplete = false;
         }
         PreviousTemp = Temp;
         PreviousExternalTransform = ExternalTransform.rotation;
 
         if (PreviousTemp == null)
         {
             return;
         }
 
     }
     public void SetRotationValues()
     {
         if (rotateX) //Checks if this axis is being used 
         {
             if (InvertX)
             {
                 rotationToSetX = (RotationDifference.x * percentageOfRotationToFollowX) * -1; // set the rotation to the value of the given transform * the percentage to follow * -1 to invert it
             }
             else
             {
                 rotationToSetX = RotationDifference.x * percentageOfRotationToFollowX; // set the rotation to the value of the given transform * the percentage to follow
             }
 
         }
         if (rotateY)
         {
             if (InvertY)
             {
                 rotationToSetY = (RotationDifference.y * percentageOfRotationToFollowY) * -1;
             }
             else
             {
                 rotationToSetY = RotationDifference.y * percentageOfRotationToFollowY;
             }
         }
         if (rotateZ)
         {
             if (InvertZ)
             {
                 rotationToSetZ = (RotationDifference.z * percentageOfRotationToFollowZ) * -1;
             }
             else
             {
                 rotationToSetZ = RotationDifference.z * percentageOfRotationToFollowZ;
             }
         }
     }
 
 
 
     public void SetRotations() //Checks which of axis' rotation the transform should follow of the given transform and applies the respective rotations.
     {
         if (RotationComplete == false)
         {
             if (rotateX && rotateY && rotateZ)
             {
                 transform.rotation *= new Quaternion(rotationToSetX, rotationToSetY, rotationToSetZ, RotationDifference.w);
             }
             else if (rotateX && rotateY)
             {
                 transform.rotation *= new Quaternion(rotationToSetX, rotationToSetY, 0f, RotationDifference.w);
             }
             else if (rotateX && rotateZ)
             {
                 transform.rotation *= new Quaternion(rotationToSetX,0f, rotationToSetZ, RotationDifference.w);
             }
             else if (rotateY && rotateZ)
             {
                 transform.rotation *= new Quaternion(0f, rotationToSetY, rotationToSetZ, RotationDifference.w);
             }
             else if (rotateX)
             {
                 transform.rotation *= new Quaternion(rotationToSetX, 0f, 0f, RotationDifference.w);
             }
             else if (rotateY)
             {
                 transform.rotation *= new Quaternion(0f, rotationToSetY, 0f, RotationDifference.w);
             }
             else if (rotateZ)
             {
                 transform.rotation *= new Quaternion(0f, 0f, rotationToSetZ, RotationDifference.w);
             }
             RotationComplete = true;
         }
     }
 
 }

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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Rotate object based on rotation of SteamVR Controller? 2 Answers

How to make any dice have a given face(int) face up? 1 Answer

Relative rotation while moving on parent. 1 Answer

Rotate multiple objects around a object, maintaining their own trajectory(not rotating their local forward vector) 1 Answer

Smooth rotation about global axis instead of local axis. 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