• 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 /
  • Help Room /
avatar image
Question by Kk_radford · Aug 15, 2016 at 01:19 AM · rotationscripting problemeuleranglesalgorithmquaternions

Trying to dynamically animate a Gimbal?

I have been trying to solve t$$anonymous$$s problem for weeks now, and I am banging my head off the wall at t$$anonymous$$s stage.

I am trying to dynamically animate a Gimbal:

alt text

I would like the centre of t$$anonymous$$s Gimble to point at an object using somet$$anonymous$$ng like transform.lookAt. I would like to ac$$anonymous$$eve t$$anonymous$$s by dynamically rotating the three rings (x,y,z) of the Gimble so that the center points in the correct direction 'naturally'.

So I have parented the rings together like so: outsideRing>middleRing>centreRing+referenceObject.

Along with the centreRing, there is also a referenceObject parented to the middle ring. T$$anonymous$$s object has Transform.LookAt active on it. My idea was to use t$$anonymous$$s reference object to find out what rotations I needed to apply to each ring in order to get the centreRing of my Gimbal facing the same way.

I thought I could measure each angle (x,y,z) between the referenceObject's direction (refRing) and the centreRing's current direction (gameObject) and apply each angle separately and concurrently to each ring of my Gimbal (outerRing = y, middleRing = z, centreRing = x) to make the centreRing arrive at it's target. (using lerp)

My script on the centreRing looks like t$$anonymous$$s:

     public float zAngle;   //the Z angle between centre ring and target
     public float yAngle;   //the Y angle between centre and target
     public float xAngle;   //the X angle between centre and the target

     public float lerpPos;   //the position along the lerp that the gameobject occupies
     public float lerpSpeed;  //the speed that the lerpPos is multiplied by
 
     public GameObject refRing;   //the reference object used to gain target direction
 
     public bool lerp = false;  //if true the lerp is active
     public bool anglePos;     //angle positive, t$$anonymous$$s tells the script in w$$anonymous$$ch direction the gameObject should rotate (+ the angle or - it)

     public bool correctRot;  //T$$anonymous$$s tells the object whether or not it should keep trying to rotate
     public bool testboolPlzIgnore;  //for testing
 
     void Update()
     {
         xAngle = Vector3.Angle(gameObject.transform.forward, -refRing.transform.forward);    //t$$anonymous$$s is the difference in degrees between the target x rotation and the current x rotation        
         zAngle = Vector3.Angle(gameObject.transform.right, refRing.transform.right);
         //yAngle = Vector3.Angle(Vector3.Cross(gameObject.transform.up, -refRing.transform.up), gameObject.transform.right);
         yAngle = Vector3.Angle(gameObject.transform.up, refRing.transform.up)
 
         if (!correctRot)                                
         {
             if (lerp)
             {
                 lerpPos += Time.deltaTime * lerpSpeed;  //increase the lerpPos float over time
 
                 if (anglePos)
                 {
                     gameObject.transform.Rotate(Mathf.Lerp(0, xAngle, lerpPos), 0, 0);  //the game object should ADD the difference in degrees to it's x axis
                 }
 
                 else if (!anglePos)
                 {
                     gameObject.transform.Rotate(Mathf.Lerp(0, -xAngle, lerpPos), 0, 0);   //the game object should SUBTRACT the difference in degrees to it's x axis
                 }               
             }
         }
 
         if(Vector3.Dot(gameObject.transform.up, -refRing.transform.forward) > 0)    //if the gameObject's up direction faces the same way as the target object's backwards direction:
         {
             anglePos = true;                                                                
             correctRot = false;
         }
 
         else if (Vector3.Dot(gameObject.transform.up, -refRing.transform.forward) < 0)   //the opposite^^
         {
             anglePos = false;
             correctRot = false;
         }
 
         if (xAngle == 0)  //if the diffence in angle is the same then set all the bools to off
         {
             correctRot = true;   //t$$anonymous$$s means the lerp bool is not being looked at
             lerp = false;   //if it was being identified it would be off because it does not need to be moved
             lerpPos = 0f;    //resets the lerpPos float back to 0%
         }
 
         else if(xAngle != 0)  //a change in angle is needed
         {
             correctRot = false;  //let the lerp be evaluated
         }
     }
 }
 


The idea is that t$$anonymous$$s script will pass Z & Y values to the outer most rings as soon as it calculates them, and then those objects have similar scripts that lerp in the same way t$$anonymous$$s one does, only using their respective Z & Y values.

So far the centreRing and the middleRing work somewhat correctly, I can turn on the lerp bool on each of those objects and they will adjust themselves into the correct position but I cannot crack the last, outer most ring. T$$anonymous$$s ring is supposed to pivot on it's Y axis and I can manually rotate the it along the Y during play time into the correct position and with both lerp bools on the middleRing and centreRing turned on the centreRing will turn to the correct position but I cannot figure out how to calculate the Y angle before hand and then apply it through code. The above method in the script to calculate yAngle just does not produce the correct angle to get those other two rings into the correct position.

I have tried many variations of the algorithm to produce the correct yAngle but I just can't seem to get it, the closest I came was:

 yAngle = Vector3.Angle(Vector3.Cross(gameObject.transform.up, -refRing.transform.up), gameObject.transform.right);

But is not it either, it's still off, (albiet slightly)

If anyone has any ideas about how to calculate that last yAngle I will be eternally grateful, OR any other method that might produce the same effect, t$$anonymous$$s might be far from the simplist/best way to do t$$anonymous$$s. (I wish there was some kind of bounty I could offer through t$$anonymous$$s site for t$$anonymous$$s question) At t$$anonymous$$s stage I just want to figure out if it's possible, I feel like it is, but my own mathematical ability is letting me down.

gimble.png (182.5 kB)
Comment
captaincheese

People who like this

1 Show 0
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.

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

88 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

Related Questions

problem setting rotation 1 Answer

transform.eulerAngles problem with parent rotation 0 Answers

How to add to a rotation angle? 0 Answers

Error when setting Rotation 2 Answers

Quaternion.RotateTowards turning the wrong way 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