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
0
Question by Drksteel · Sep 25, 2017 at 05:27 PM · rotationrotate objectrotation axisrotatetowards

Having trouble rotating a turret

I need to rotate a turret around the Z axis. The turret is successfully rotating around the z axis however, the turrets x rotation becomes 0 when it should stay at -90, and i cannot figure out how to rotate the turret and keep the x rotation at -90. Here is the code i have to rotate the turret.`public class PT_Turret_rotate : MonoBehaviour {

 public GameObject player;

 private Vector3 lookDir;

 private void Update()
 {
     lookDir = player.transform.position - transform.position;
     lookDir.y = 0;
     transform.rotation = Quaternion.LookRotation(lookDir);
 }

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
0

Answer by MacDx · Sep 25, 2017 at 06:06 PM

I think the problem is the vector you are passing to the LookRotation method. You said that you:

need to rotate a turret around the Z axis

This means that you will only move in Z degrees and X and Y degrees should always be 0. However, you are passing a vector that will result in a rotation around the Y axis (depending on how the player can move). So what you need to is leave the X and Y values as they are and set the Z value to zero instead.

Comment
Add comment · Show 5 · 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 Whitecold · Sep 25, 2017 at 06:15 PM 0
Share

In unity, up is the y axis. So if you are using standard unity orientation, you want to only change the y rotation value

avatar image Drksteel · Sep 25, 2017 at 06:49 PM 0
Share

alt text

I tried your answer and the effect was unfortunately not helpful I made an illustration in hopes of you seeing the problem. The turret always ends on its side and rotates with the base and not the top. even if i leave the Z axis at zero the turret still ends up sideways and facing the opposite direction of the player. Even when rotating it on the X axis it still is sideways. could it be an error with the model?

image.png (27.1 kB)
avatar image MacDx Drksteel · Sep 25, 2017 at 07:31 PM 0
Share

Ahh I see, so (assu$$anonymous$$g your camera is pointing downwards in your scene, hence the top view) you don't really want to rotate around Z axis, what you really want is rotation around the Y axis. In that case, yeah, my answer won't help. Give me a few $$anonymous$$utes to answer with this new info in $$anonymous$$d.

avatar image MacDx Drksteel · Sep 25, 2017 at 08:32 PM 0
Share

Ok Drksteel, I set up a project with a camera facing down ( x rotation to 90 degrees) I placed a player that can move in x and z direction and I placed a turret with exactly the same script you are showing here and it works perfectly. Either you have a different setup from what I am assu$$anonymous$$g or your player movement is causing the problem.

I'll zip and post a link to the project so you can check it out:

Edit: Here it is https://drive.google.com/open?id=0Bx9pF2CVhb6pVjdYTWNjcThWN00

avatar image Drksteel MacDx · Sep 26, 2017 at 04:07 PM 0
Share

I have figured out that the error is the model and not the scripting, the code is working perfectly fine its just the turret's base rotation is actually just sideways. Thank you for helping me figure that out.

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

97 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is this rotation not performed as expected? 1 Answer

The object turn but the axis dont (SOLVED) 3 Answers

Smooth Camera Rotation After 2 Seconds 1 Answer

Question on Forcing a Specific Rotation 1 Answer

Vector3.RotateTowards Problem 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