Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Lord Simpson · Aug 22, 2011 at 04:54 PM · rotationquaternionlookateulerangles

Quaternion Equivalent of restricting an axes rotation to zero

Hi I have a setup where a turret is made up of two gameobjects, one being the base that should only rotate on the Y axis and a head which is a child of the base which should only rotate in the X axis.. The problem im experiencing is that to restrict the base to the y axis i convert the Quterion rotation of the head into euler angles which seams to either cause a loss of accuracy or for some other reason instead of facing a moving target it aims just to one side then next frame just a little to the other side

 turrethead.rotation = Quaternion.RotateTowards (turrethead.rotation, Quaternion.LookRotation (gooddirection, transform.up), turnspeed * Time.deltaTime);
 Vector3 rot = turrethead.localEulerAngles;
 rot.x = 0;
 rot.z = 0;
 turretbase.localEulerAngles = rot; [commenting out this line fixes things]

what I think I need is instead of using Euler angles is to restrict the rotation using pure quaternion maths but cant seam to find anything on how to remove the "x and z" rotation from a quaternion Any help would be 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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Lord Simpson · Aug 22, 2011 at 10:37 PM

OK simplified things...was thinking to much in reality. The Most important thing is the head faces the target and really the base is just graphical fluff so removed the parenting and used

 turrethead.rotation = Quaternion.RotateTowards(turrethead.rotation,Quaternion.LookRotation(gooddirection,transform.up),turnspeed*Time.deltaTime);
 turretbase.localEulerAngles = new Vector3(0,turrethead.localEulerAngles.y,0);

Works Perfectly :D

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 Bunny83 · Aug 23, 2011 at 12:27 AM 0
Share

Of course that would work as well ;) I was thinking about that but a lot people won't get it so i posted a real world example :D

avatar image
1

Answer by Bunny83 · Aug 22, 2011 at 05:34 PM

Quaternions don't work in 3 axis like the eulerangles representation. It's more a combination of an internal vector3 that specifies a rotation axis and the rotation around this axis.

Your problem is that you rotate the head before the base and you rotate both objects in worldspace. So rotating the head towards the target in worldspace and after that rotating the base would also rotate the head since it's a child of the base. Therefore your head points into the wrong direction which causes the procedure to repeat each frame.

I guess i would do it like this:

vector3 xzDirection = gooddirection; xzDirection.y = 0.0f;

turretbase.rotation = Quaternion.RotateTowards (turretbase.rotation, Quaternion.LookRotation (xzDirection, Vector3.up), turnspeed * Time.deltaTime);

float pitchAngle = Mathf.Acos() * Mathf.Rad2Deg; turrethead.localRotation = Quaternion.Euler(pitchAngle,0,0);

Not tested! Maybe you need to invert the pitchAngle

Comment
Add comment · Show 2 · 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 Lord Simpson · Aug 22, 2011 at 08:53 PM 0
Share

[EDIT] Ah found a problem

Thank you very much!! just needed A little tweaking but I think this is the correct version of what you proposed as it hasent gone wrong yet :D

Vector3 xzDirection = gooddirection; xzDirection.y = 0.0f;

turretbase.rotation = Quaternion.RotateTowards (turretbase.rotation, Quaternion.LookRotation (xzDirection, Vector3.up), turnspeed * Time.deltaTime);

float pitchAngle = -$$anonymous$$athf.Asin((gooddirection).y/gooddirection.magnitude)*$$anonymous$$athf.Rad2Deg;

pitchAngle = $$anonymous$$athf.$$anonymous$$oveTowardsAngle(turrethead.localEulerAngles.x,pitchAngle,turnspeed*Time.deltaTime);

turrethead.localRotation = Quaternion.Euler(pitchAngle,0,0);

avatar image Lord Simpson · Aug 22, 2011 at 09:54 PM 0
Share

Ok just one problem that i cant seam to fix..The turret is actually the child of an object that can rotate in any direction..everything works fine BUT the turret base keeps its up pointed to world up and not its parents up...cant seam to sort this problem either :@

avatar image
0

Answer by daniellasry · Aug 22, 2011 at 05:05 PM

Why not use one of the Transform.Rotate() variants to rotate around just one axis?

Say for example:

 //This will rotate the turrethead by 90 degrees around the x axis
 turrethead.Rotate(90.0f, 0.0f, 0.0f);
 
 //This will rotate the turret base by 45 degrees around the y axis
 turretbase.Rotate(0.0f, 45.0f, 0.0f);
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 Bunny83 · Aug 22, 2011 at 05:36 PM 0
Share

Rotate() rotates the transform relative to the current rotation. He wants the turret to aim at a specific target ;)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

LookAt on only 1 axis for 2D games? 1 Answer

Clarifying the ambiguities of rotation in Unity 1 Answer

Rotating local transform not rotating fully if tranform.up is not exactly at (0,1,0) 1 Answer

Match Y axis rotation. 1 Answer

Change rotation of an object based on an HTC Vive Controller 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