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 PsychoDuckArcade · Jun 17, 2014 at 11:41 PM · rotationrotatedirectionvectorunit

How should I rotate a 2D unit vector by X degrees?

As the title says, I have a 2D unit vector, and I simply need to rotate it by X degrees. I've done a lot of googling, and couldn't really find what I needed (most similar problems I found were quite a bit more complex).

Also I'd be calling this up to 4 times per update, so I'm hoping for a solution that isn't horrible on performance.

Oh and X degrees is actually coming from the camera's Y rotation. So if there's a better way to offset the vector by the camera's Y rotation that involves not using degrees, I suppose that'd be just as useful. (And by better I mean more efficient.)

Tried multiplying my vector like the following as suggested online: Quaternion.AngleAxis(Camera.main.transform.rotation.y, Vector3.forward) * new Vector2(Input.GetAxis("P1MoveHorizontal"), Input.GetAxis("P1MoveVertical"))

Didn't make any difference. And

 Quaternion.AngleAxis(Camera.main.transform.rotation.y, Vector3.forward) * new Vector2(Input.GetAxis("P1MoveHorizontal"), Input.GetAxis("P1MoveVertical"))

Gives me an error where Quaternion cannot multiply with a vector2.

Tried this:

 float sin = Mathf.Sin(Camera.main.transform.rotation.y);
         float cos = Mathf.Cos(Camera.main.transform.rotation.y);
 
         float tx = Input.GetAxis("P1MoveHorizontal");
         float ty = Input.GetAxis("P1MoveVertical");
         Move(new Vector2((cos * tx) - (sin * ty), (cos * ty) + (sin * tx)));

Made a difference, but still not the correct directions.

I even tried convert the unit vector to degrees, adding the camera's rotation in degrees, and then converting it back into a vector:

 float directionAngle = Mathf.Atan2(Input.GetAxis("P1MoveVertical"), Input.GetAxis("P1MoveHorizontal"));
         directionAngle += Camera.main.transform.rotation.y;
         Move(new Vector2(Mathf.Sin(directionAngle), Mathf.Cos(directionAngle)));

But EVEN THIS which I was certain would work but was hoping to avoid for inefficiency, is not working at all as expected. Without even inputting a direction my object moves around in circles and wanders off the scene.

Comment
Add comment · Show 2
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 PouletFrit · Jun 18, 2014 at 03:57 AM 0
Share

Your first try, the one multiplying a quaternion with a vector should had work... Also I dont get any error when I multiply a quaternion by a vector2... What version of Unity are you using?

And if you want to rotate around the Y axis of the camera you should use transform.up and not forward. Also you talked about performance, so I suggest you to cache your camera transform

avatar image PsychoDuckArcade · Jun 18, 2014 at 04:08 PM 0
Share

Thanks for the answer. I Tried motherBoard.mover.$$anonymous$$ove(Quaternion.AngleAxis(Camera.main.transform.rotation.eulerAngles.y, Vector3.up) * new Vector2(Input.GetAxis("P1$$anonymous$$oveHorizontal"), Input.GetAxis("P1$$anonymous$$oveVertical")));

and

 motherBoard.mover.$$anonymous$$ove(Quaternion.AngleAxis(Camera.main.transform.rotation.eulerAngles.y, Camera.main.transform.up) * new Vector2(Input.GetAxis("P1$$anonymous$$oveHorizontal"), Input.GetAxis("P1$$anonymous$$oveVertical")));

But it made no difference, movement still occurs relative to world not the camera.

Also just to point out, I started using rotation.eulerAngles.y ins$$anonymous$$d of rotation.y as, according to another user, rotation.y is something else..

1 Reply

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

Answer by PsychoDuckArcade · Jun 18, 2014 at 05:30 PM

Solved.

Converted angle from degrees to radians, since apparently sin and cos take radians despite using degrees in school, and then made sin negative since apparently the sign that sin returns isn't always consistent between math libraries.

 float x = Input.GetAxis("P1MoveHorizontal");
         float y = Input.GetAxis("P1MoveVertical");
         float sinAngle = -Mathf.Sin(Mathf.Deg2Rad * Camera.main.transform.rotation.eulerAngles.y);
         float cosAngle = Mathf.Cos(Mathf.Deg2Rad * Camera.main.transform.rotation.eulerAngles.y);
         Move(new Vector2(x * cosAngle - y * sinAngle, y * cosAngle + x * sinAngle));
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

2 People are following this question.

avatar image avatar image

Related Questions

Change input direction to camera's forward 0 Answers

Rotate a GameObject [set front] 1 Answer

Creating Hint Vector 1 Answer

Object rotation to unit vector 1 Answer

(Picture for Clarity) How do you click on an object, and from that hit point, calculate a direction vector, and then make that direction vector be the upwards direction of an object? 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