Skip to main content

Posts

Showing posts from August, 2016

How to rotate a 2D Vector in unity

There are hundreds of posts on this topic, but i will specifically stick to unity only. Since i struggle a little bit to figure out how vector's work's in unity, so i don't want this to happen with you guy's.


Few day's back i was simply trying to rotate a 2D vector in unity , just to create a circle.

I found this equation in wikipedia , which rotates a vector




First i tried to rotate a simple vector which was pointing at (2,0) to 90 degrees at (0,2), take a look at the image :




So i took the equation, and tried it in unity, which looks like this :

publicVector2RotateVector(Vector2v,floatangle)
{
float_x=v.x*Mathf.Cos(angle)-v.y*Mathf.Sin(angle);
float_y=v.x*Mathf.Sin(angle) +v.y*Mathf.Cos(angle);
returnnewVector2(_x,_y);
}

If i pass RotateVector(new Vector2(2,0), 90), I should get a Vector(0,2) but unfortunately i was getting something like this (-0.9, 1.8) .



If you read the Unity Documents carefully they have said that Sin and Cos function takes the value in Radian, so w…