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 kipenson · Feb 20, 2014 at 06:31 PM · 2drotationturret

2d turret problem

Hello,

In my 2d game(player moves along x and y) i have a turret that rotates on z axis (so it's like a clock); when the player walks under it, the turret follows correctly, but when you get at it's level (the same height as the turret's position - so like about 9 o'clock and also 3 o'clock in the right side) it starts to twist (still pointing at the player but also rotates on the x axis) here is my code:

 if (targetT) {
 
   Vector3 lookDirection = targetT.transform.position - transform.position;
 
   lookDirection.z = 0;
 
   Quaternion targetRotation = Quaternion.LookRotation(lookDirection, Vector3.right);
 
   transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * damp);
 
 //transform.rotation = Quaternion.LookRotation(lookDirection);//works but there is no damp
 
 }

on this line: Quaternion targetRotation = Quaternion.LookRotation(lookDirection, Vector3.right); if i take away the: Vector3.right part the problem remains but it happens on 12 and 6 o'clock; how can i spin this turret correctly all around ? or if there is a way to damp the rotation(last line of code) etc. Thank you for your time

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

Answer by robertbu · Feb 20, 2014 at 06:35 PM

Try this:

 if (targetT) {
   Vector3 lookDirection = targetT.transform.position - transform.position;
   float angle = mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg;
   Quaternion targetRotation = Quaternion.AngleAxis(angle, Vector3.forward);
   transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * damp);
 }

This code assumes that 'forward' is the right side of your sprite. If it is not, you need to either edit your texture to make it the right side, or modify the angle before doing the AngleAxis() call.

Comment
Add comment · Show 3 · 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 kipenson · Feb 20, 2014 at 08:17 PM 0
Share

it works, have to reorient the sprite and other things, but this helps a lot; Thank you very much !

avatar image kipenson · Feb 20, 2014 at 08:25 PM 0
Share

thank you very much, this helps a lot, have to reorient the sprite and other things, but it works; Thanks

avatar image getyour411 · Feb 20, 2014 at 08:26 PM 0
Share

Please dont' post updates/thank-you notes as another Answer (I converted this one to a comment for you). Ins$$anonymous$$d, click the 'Accept Solution' icon underneath the thumbs next to robertbu's Answer.

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

19 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

Related Questions

Turret bullet rotation problem 1 Answer

2D sprite rotation with velocity and interpolation 1 Answer

Smooth 2D rotation unexpected 1 Answer

How to rotate 2D object to location of joystick? 1 Answer

2d Movement Messes up when cube is rotatated 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