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 LunguM · May 12, 2017 at 06:23 PM · rotationvector3quaternioneulerangleslookrotation

Turret rotation on one axis problems

I've been trying to make a turret (1 of its parts) rotate on only one axis, namely the Y axis. However, I've had no luck in doing so and solutions found online did not me help fix the problem. So I came to the last place where I might get some help.

 Quaternion lookRotation = Quaternion.LookRotation(targetInterceptPos - PartToRotate.position, Vector3.up);
 Vector3 rotation = Quaternion.Slerp(PartToRotate.rotation, lookRotation, Time.deltaTime * BaseTurnSpeed).eulerAngles;
 PartToRotate.rotation = Quaternion.Euler(0f, rotation.y, 0f);

The base of the turret (named PartToRotate) should rotate only on the Y axis. However, if the parent of the turret (a ship) starts turning, lets say to (0, 0, 45) then the turret will rotate on the X and Z axis to accomodate this rotation for some reason.

alt text

If I try to use localRotation, the turrets don't even rotate on the Y axis anymore when the ship starts to move.

"targetInterceptPos" is a Vector3 variable used for a look ahead calculation. If need be I can post the code for that as well.

Thank you.

rotation.jpg (155.9 kB)
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

2 Replies

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

Answer by LunguM · May 13, 2017 at 09:59 AM

I've managed to solve it I think. The turrets no longer rotate on the X or Z axis. If anyone stumbles upon this question and has a better solution feel free to share it, for everyone else, here is the code that fixed it for me, maybe it will help you too.

Keep in mind that I wrote the code with a 2 part turret in mind, one which contains a base rotator and the turret itself.

 //First part - Rotates the base of the turret
 Quaternion lookRotation = Quaternion.LookRotation(targetInterceptPos - PartToRotate.position, Vector3.up);
 PartToRotate.rotation = Quaternion.RotateTowards(PartToRotate.rotation, lookRotation, Time.deltaTime * BaseTurnSpeed);
 PartToRotate.localEulerAngles = new Vector3(0, PartToRotate.localEulerAngles.y, 0);
 
 //Second part - Rotates the turret itself
 Quaternion turretLookRotation = Quaternion.LookRotation(targetInterceptPos - TurretRotator.position);
 if (TurretRotator.localEulerAngles.x > 270)
 {
  TurretRotator.rotation = Quaternion.RotateTowards(TurretRotator.rotation, turretLookRotation, Time.deltaTime * TurretTurnSpeed);
  TurretRotator.localEulerAngles = new Vector3(TurretRotator.localEulerAngles.x, 0, 0);
  }
 else if (TurretRotator.localEulerAngles.x < 10)
  {
   TurretRotator.rotation = Quaternion.RotateTowards(TurretRotator.rotation, turretLookRotation, Time.deltaTime * TurretTurnSpeed);
   TurretRotator.localEulerAngles = new Vector3(TurretRotator.localEulerAngles.x, 0, 0);
   }
Comment
Add comment · 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
0

Answer by leech54 · May 14, 2017 at 04:43 PM

I would try a simple azimuth calculation for it.

 float angle = Mathf.Atan2(target.transform.position.x - partToRotate.position.x, target.transform.position.z - partToRotate.position.z) * Mathf.Red2Deg;
 
 transform.rotation = Quaternion.Euler(0f,angle,0f);
 
 you could slerp to this angle once you see how it works.
Comment
Add comment · 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

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

86 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

Related Questions

Quaternion.LookRotation and Vector3.SmoothDamp Problems 1 Answer

c# modify only one axis of a quaternion 2 Answers

How is the rotation of a Transform converted into a Vector3 in the inspector ? 2 Answers

LookRotation but keeping oriented a certain way 0 Answers

Slerp to make the right/left side face another object 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