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 RyanAppel · Jul 21, 2013 at 09:04 PM ·

Why am I getting such strange rotation values when using Quaternion.Euler?

I am trying to rotate the top section of a tank model (parented to the base of the tank) towards a target (the player), and then similarly rotate the cannon (parented to the top section of the tank) up and down to target the player. The top section of the tank rotates like it should, but I'm getting strange results for the cannon. Here's the code... Please help!

 // Top section of the tank
 Vector3 direction = _targetTransform.position - TopTransform.position;
         
 float y = Mathf.Atan2(-direction.z, direction.x) * Mathf.Rad2Deg - 90.0f;
         
 Quaternion topRotation = Quaternion.Euler(0.0f, y, 0.0f);
         
 TopTransform.rotation = topRotation;
         
         
 // Cannon
 direction = _targetTransform.position - CannonTransform.position;
         
 float x = Mathf.Atan2(direction.y, direction.z) * Mathf.Rad2Deg;
                 
 CannonTransform.rotation = Quaternion.Euler(x, y, 0.0f);

 
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
2
Best Answer

Answer by Owen-Reynolds · Jul 21, 2013 at 09:24 PM

The math (2nd last last line) appears to be incorrect. If you assume the cannon is y-rotated towards the target, you're solving the triangle: elevation / distance-along-ground. Distance along ground is local z, but you only have world. Instead of direction.z should have flat distance: sqrt(x^2 + z^2.)

I'm guessing the code now works, or at least makes more sense, if you are pointed North (where local z is global z.)

Also, Unity thinks x-rotation is a forwards roll (+45 aims down, not up,) so may need *-1 in there.

Then, sometimes easier to set Cannon.localRotation=Q.E(x,0,0) (your code has the barrel repeat the turret spin, which should work since Unity does y,x,z, but might be confusing.)

Comment
Add comment · Show 1 · 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
avatar image RyanAppel · Jul 21, 2013 at 09:49 PM 0
Share

Thank you so much! Here is my working code for anyone with a similar problem...

     private void AimAtTarget()
     {
         // Top section of the tank
         Vector3 direction = _targetTransform.position - TopTransform.position;
         
         float y = $$anonymous$$athf.Atan2(-direction.z, direction.x) * $$anonymous$$athf.Rad2Deg - 90.0f;
         
         Quaternion topRotation = Quaternion.Euler(0.0f, y, 0.0f);
             
         float t = _top$$anonymous$$oveSpeed * Time.deltaTime;
         
         TopTransform.rotation = Quaternion.Slerp(TopTransform.rotation, topRotation, t);
         
         
         // Cannon
         direction = _targetTransform.position - CannonTransform.position;
         
         float x = $$anonymous$$athf.Atan2(direction.y, $$anonymous$$athf.Sqrt(direction.x * direction.x + direction.z * direction.z)) * $$anonymous$$athf.Rad2Deg;
         
         t = _cannon$$anonymous$$oveSpeed * Time.deltaTime;
         
         Quaternion cannonRotation = Quaternion.Euler(x - CANNON_X_OFFSET, 0.0f, 0.0f);
                 
         CannonTransform.localRotation = Quaternion.Slerp(CannonTransform.localRotation, cannonRotation, t);
     }

I didn't need to negate the X (don't know why). For anyone wondering about CANNON_X_OFFSET, my tank model had the barrel angled up a few degrees. Thanks again Owen!

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

16 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

Related Questions

Help me please im stuck here Assets/MeleeSystem.js(12,38): UCE0001: ';' expected. Insert a semicolon at the end. 1 Answer

Add force based on swipe speed, direction, curve 3 Answers

Script has Not Finished Compiling Error 0 Answers

Loading Bar Help 0 Answers

EnemyAi script 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