• 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 zero3growlithe · Aug 20, 2014 at 08:58 PM · rotationquaternionoffset

Mimic other object's rotation on one axis (with offset)

So, I have an object which is a ship and the other object which is its external "helper" axis for the ship's animations to be possible. What I'm trying to do is to have:

  • the external axis to inherit x-axis rotation from the ship's x-axis rotation

  • the ship to inherit y-axis rotation from the external axis's y-axis

  • and ship's z-axis to inherit the z-axis rotation from external axis while offseting it for banking purposes (as befits for an anti-grav ship)

and the problem I have is that in some cases a gimbal lock occurs on the z-axis (because here I'm using Quaternion.eulerAngles to offset the rotation on z-axis for banking), I know quaternions is the solution here but I have no idea how to implement it and Quaternion.eulerAngles seem to be exactly the same as localEulerAngles. Since my ship can fly on up-side down surfaces or perpendicullary to World's up axis the gimbal lock is unavoidable.

Here's some of my code as image with some red guides: http://prntscr.com/4esc03

Whole function's code:

 // Turning controls
     @HideInInspector var HorizAxisInputVar : float;
     private var HorizAxisInputLerpSpeed: float;
     private var HorizAxisInputVarSmooth : float;
     private var HorizBankVar : float;
     private var AirBreaksAdd : float;
     private var AirbreaksBank : float;
     private var airbrakesRotation : Vector2;
     private var turningSpeed : float;
 function ShipTurning (){
 // Simulate axis for smooth analog and keyboard input
     var axisLerpSpeed : float = 10;
     var maxAxisLerpSpeed : float = 0;
     if (USER != PL || USER == "Autopilot") 
         axisLerpSpeed = 20;
     if (Mathf.Abs(HorizAxisInput) > 0.1) 
         maxAxisLerpSpeed = 8;
     HorizAxisInputLerpSpeed = Mathf.MoveTowards (HorizAxisInputLerpSpeed, maxAxisLerpSpeed, Time.fixedDeltaTime * axisLerpSpeed * 2);
     if (HorizAxisInput > 0.05 || HorizAxisInput < -0.05){
         HorizAxisInputVar = Mathf.MoveTowards (HorizAxisInputVar, HorizAxisInput, Time.fixedDeltaTime * HorizAxisInputLerpSpeed);
     } else {HorizAxisInputVar = Mathf.MoveTowards (HorizAxisInputVar, 0, Time.fixedDeltaTime * axisLerpSpeed);}
     // Analog input variable
         HorizAxisInputVar = Mathf.Clamp (HorizAxisInputVar, -1, 1);
         HorizAxisInputVarSmooth = Mathf.Lerp (HorizAxisInputVarSmooth, HorizAxisInputVar, Time.fixedDeltaTime * (axisLerpSpeed-1));
     if (USER != PL || USER == "Autopilot")
         HorizAxisInputVarSmooth = HorizAxisInputVar;
     turningSpeed = ((HorizAxisInputVarSmooth * Handling) + AirBreaksAdd) * Time.fixedDeltaTime;
 // Ship banking
     var bankAngle : float;
     if (!BRTotalRotation || BRTotalRotation == 360){
         HorizBankVar = Mathf.Lerp(HorizBankVar, HorizAxisInputVarSmooth, Time.fixedDeltaTime * 2);
         if (!BRTotalRotation)
             bankAngle = (-HorizBankVar * 35) + AirbreaksBank + shipStandbyAnimation.z;
     }
 // Axises rotations
     if (ShipOnMagStrip){
         PlayerShipExternalAxis.Rotate(
             -Vector3.Dot(myTransform.forward, PlayerShipExternalAxis.up) * 45,
             turningSpeed,
             0
         );
         myTransform.Rotate(0, -Vector3.Dot(PlayerShipExternalAxis.forward, -myTransform.right) * 10, 0);
         /*myTransform.Rotate(
             myTransform.up * Mathf.Sign(-Vector3.Dot(PlayerShipExternalAxis.forward, -myTransform.right)) *
             Quaternion.Angle(myTransform.rotation, PlayerShipExternalAxis.rotation)
         );*/
         /*myTransform.rotation = Quaternion.LookRotation(
             PlayerShipExternalAxis.forward,
             myTransform.up);*/
         // myTransform.Rotate(0, -Vector3.Dot(PlayerShipExternalAxis.forward, -myTransform.right) * 45, 0);
         myTransform.rotation.eulerAngles.z = PlayerShipExternalAxis.rotation.eulerAngles.z + bankAngle;
     } else {
         PlayerShipExternalAxis.localEulerAngles.x = myTransform.localEulerAngles.x;
         PlayerShipExternalAxis.localEulerAngles.y += turningSpeed;
         myTransform.localEulerAngles.y = PlayerShipExternalAxis.localEulerAngles.y;
         if (BRTotalRotation == 0)
             myTransform.rotation.eulerAngles.z = PlayerShipExternalAxis.rotation.eulerAngles.z;
     }
 // Control additional axis for parallel ship rotation
     if (AGRayHitDistance <= shipInAirLimit)
         AdditionalAxis.localEulerAngles.z = myTransform.localEulerAngles.z;
 // Ship inAir momentum variable
     if (AGRayHitDistance > shipInAirLimit){
         OffGroundRotationChange += turningSpeed;
     } else OffGroundRotationChange = 0;
 }


Any help is appreciated!

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.

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

21 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

Related Questions

Limit Rotation of Camera around a player using keyboard 1 Answer

Turning a vector into a local offset? Math help 2 Answers

Location in relation to other objects rotation 1 Answer

Quaternion partial offset, need help badly 0 Answers

Give the Quaternion Rotation an offset of 90° 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