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 Qiut · Aug 20, 2012 at 04:14 PM · 2drotationmovementquaternion

Rotating an object to target on the Z axis

I'm building a 2d game with airplanes, using the 2d-toolkit. I'm trying make and AI plane which would follow the player by rotating and moving towards him (I want it to only rotate on the Z axis), I'm using:

 var deltaRotation = target.position - transform.position;

 var rotation = Quaternion.LookRotation(deltaRotation);

 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 3);



to rotate the plane: and this, to move it:

   var direction: Vector3 = Vector3(speed, 0.0, 0.0);

         transform.Translate(direction);



but the plane only rotates around the player using the X and Y axis, if the player move down/up it doesn't rotate on the Z axis.

And if I try to restrict the rotation to the Z axis by:

  deltaRotation.y = 0;
  deltaRotation.x = 0;


it doesn't rotate at all.

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 whydoidoit · Aug 20, 2012 at 04:17 PM

To start with you shouldn't go messing with the .x .y .z and .w properties of a Quaternion - they don't mean what you think they do and are a representation of the rotation in 4d space.

You could try getting the eulerAngles from the Quaternion.LookRotation and zero out the .x and .y in that before converting it back into a Quaternion.

 var rotation = Quaternion.LookRotation(deltaRotation).eulerAngles;
 rotation.x = rotation.y = 0;
 var rotatingQuaternion = Quaternion.Euler(rotation);


 
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 Qiut · Aug 21, 2012 at 08:44 AM 0
Share

Thanks, but it still doesn't rotate towards it's target on the Z axis, when I output rotation.z to the screen using the GUI it stays 0 all the time no matter where I move the target. If I remove rotation.x = rotation.y = 0; it rotates on the other axis, though.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotating an object towards target on a single axis 2 Answers

Help with rotating a sprite with mouse. 2 Answers

Rotate/Flip Image in Unity 2d using Virtual Joystick 1 Answer

Unity 2D: How can i get my sprite to rotate to the direction I am going? 1 Answer

2D Simulated Sword Swing - Sword Floating Away 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