Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Griffo · Mar 24, 2013 at 09:01 AM · euleranglesfromtorotation

Return to original position using eulerAngles & FromToRotation

I have a model of a Mech, the problem is that the original models axis are incorrect so have to compensate when tracking the target with -

 rotation *= Quaternion.FromToRotation(Vector3.up, Vector3.right);

So when I use this script to return the turret back to match the lower half of the Mech with the above line in, it never yields because it mess the eulerAngles I presume, without the line in it rotates and yields but with the Mechs head out of orientation, so how would I correct mechTurret.eulerAngles to take into account the Quaternion.FromToRotation.

If I use an empty game object to correct it works but I loose the animations on the turret, so thats not an option, any help would be great, thanks.

 function rotateTurret(){
 
     while((mechTurret.eulerAngles - transform.eulerAngles).sqrMagnitude > 0.01f){
 
 // Rotate the Mech Turret to face the direction of the bottom half of the Mech
        var lookPos = transform.forward;
         var rotation = Quaternion.LookRotation(lookPos);
 
         rotation *= Quaternion.FromToRotation(Vector3.up, Vector3.right);
 
         mechTurret.rotation = Quaternion.Slerp(mechTurret.rotation, rotation, Time.deltaTime * 1);
         yield;
     }
 }
Comment
Add comment · Show 3
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 Fattie · Mar 24, 2013 at 10:51 AM 0
Share

just purely FYI. it would be really advisable to not use the variable name "rotation" as it is a property

could you make it something more descriptive like "desiredTarget" or the like?

avatar image Fattie · Mar 24, 2013 at 10:52 AM 0
Share

I don't totally understand what you're doing, but could the problem relate to:

you are using global up to rotate it around, rather than it's local up?

Just also, would it be tremendously easier to just use "RotateAround" here?

avatar image Griffo · Mar 24, 2013 at 12:13 PM 0
Share

Fattie, thanks for the input, what I have is a function for the Turret of a $$anonymous$$ech walker to track the player when he gets within a detection range -

 function turretTrackPlayer(){
 
 // Rotate turret to face the Player
     var lookPos = player.position - mechTurret.position;
     var rotation = Quaternion.LookRotation(lookPos);
 
 // Next 2 line corrects the original axis being incorrect            
     rotation *= Quaternion.FromToRotation(Vector3.up, Vector3.right);
     rotation *= Quaternion.Euler(90, 0, 0);
 
     mechTurret.rotation = Quaternion.Slerp(mechTurret.rotation, rotation, Time.deltaTime * turnToLookSpeed);
 }

Then when the player goes out of the detection range I wanted the turret to return back to match the rotation of the bottom half, the legs of the $$anonymous$$ech walker, this is where I'm stuck, because of the two correction lines of code it throws the eulerAngles off, this is the only way I thought I knew how to do it with some help from whydoidoit, this is the script to return the turret back into line with the bottom half, the legs -

 function rotateTurret(){
 
     while((mechTurret.eulerAngles - transform.eulerAngles).sqr$$anonymous$$agnitude > 0.01f){
 
 // Rotate the $$anonymous$$ech Turret to face the direction of the bottom half of the $$anonymous$$ech
         var lookPos = transform.forward;
         var rotation = Quaternion.LookRotation(lookPos);
 
         rotation *= Quaternion.FromToRotation(Vector3.up, Vector3.right);
         rotation *= Quaternion.Euler(90, 0, 0);
 
         mechTurret.rotation = Quaternion.Slerp(mechTurret.rotation, rotation, Time.deltaTime * 1);
         yield;
     }
 }

I maybe using the wrong formula, to some this may be a stupid error and know of an easy solution but it's got me, my head hurts .. :)

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

11 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

Related Questions

Question regarding - Quaternion.FromToRotation, Walking around a cube 0 Answers

Head and body rotate threshold 1 Answer

How to get inspector rotation values ? 2 Answers

Rigidbody2D constraints checked for x and y but still moves when character jumps on it from distance 0 Answers

Player Rotation while walking on wall 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