- Home /
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.
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
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..
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));
Your answer
Follow this Question
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