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 Garan105 · Jan 23, 2014 at 05:10 PM · unity 2dquaternion

How to use rotations in 2D?

When using quaternions in 2D, only one of the axis are used. How do I translate the normal one axis rotations used in 2D games to the quaternion used by the Sprite.transform.rotation? I'm specifically thinking of instantiating a missile in a direction, but I don't know what rotation to use.

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

Answer by robertbu · Jan 23, 2014 at 05:21 PM

Quaternion.AngleAxis() works. Use Vector3.forward as the axis. Given a vector from your origin, Mathf.Atan2 will calculate an angle. Vector3.right is the 0.0 degree reference for that rotation. Atan2() returns radians, so be sure to use Mathf.Rad2Deg with the result.

If you are using 3D objects in a 2D setup where the 'forward' of those objects aim a positive 'z' when the roation is (0,0,0), then Transform.LookAt() and Quaternion.LookRotation() will work. Both functions have an optional second parameter that defines 'up'. For 2D on the XY plane, you need this optional second parameter, and it should be set to Vector3.forward.

So if you have a direction for your missle, the code might look like:

 var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.AngleAxis(angle, Vectore3.forward);

This assumes that the head of the missile points towards Vector3.right when the rotation is (0,0,0).

If you are using a 3D missile of the correct rotation, then the code might be:

 transform.rotation = Quaternion.LookRotation(dir, Vector3.forward);
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 Garan105 · Jan 23, 2014 at 05:31 PM 0
Share

Thanks! Just tried it and it works.

avatar image Bluegru · Nov 02, 2018 at 04:34 PM 0
Share

Had the same Problem, and this worked perfectly fine, However if I test this in $$anonymous$$ultiplayer, it only works for the Host, not the client (with dedicated server no client works), Its really funny to watch, as it looks like it tries to work, but is just a bit off, Any Idea on what causes this problem?


Edit: seems like the differenc in screenresolution is the problem, as I use

 Vector2 targetPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
         targetPosition = cam.ScreenToWorldPoint(targetPosition);

to get the position of the target and if the clients use a different one as the server, the calculations are a bit off. It works if all clients use fullscreen (only tested on my PC with only one monitor)

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

Rigidbody 2d rotation will count as float 0 Answers

How to rotate transform only on its Y axis? 1 Answer

What Should i use rigidbody.AddTorque() or Quaternions ? 1 Answer

How can I instantiate a gameobject facing another gameobject 2D? 0 Answers

Rotate Ball with high speed rotation to slow speed 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