converting velocity to angle/magnitude?

I’m trying to create a projectile class. I want it to simply move in the direction it is given at the speed it is given. (And this is 2D so I need only one variable for angle.)

But what is the function that can take an angle and magnitude and convert it into a vector2 position, which is what I need to feed the velocity? I can see in the documentation that vector2 has helper functions that can convert a vector2 position into angle and magnitude, but I want to convert it the other way around. How do I do this?

well is x = y/t; then y = 1/x*t;
cant you just move the variables around so that it satisfies your equation?

well a vector is direction and magnitude.

This is stored as however many dimensions there are. 1 float for each dimension in the case of a vector.

If you wanted to store say 2d as a 1d angle, you’re losing 1 dimension worth of data. So to convert back you have to make assumptions. For instance we could say “our angle is the degrees counter-clockwise off the positive x axis”.

Well, we have a method who does that very thing.

float a = Mathf.Atan2(v.y, v.x);
float mag = v.magnitude;

That’s the angle off the positive x axis in 2d counter-clockwise.

To convert back, it’s as simple as this trig:

var v = new Vector2(Mathf.Cos(a), Mathf.Sin(a)) * mag;

Here’s the thing, BOTH vector (cartesian coords) and angle:mag (polar coords) require 2 floats to store them. And the vector is the system that velocity in unity expects. So it’s best to store it as such, since it’s the same amount of storage space… but it requires no trig to convert.

But angle:mag is good for displaying to the person who is the designer.

If this is just for ease of reading by the designer. Create something like a custom editor script that displays it as such to the designer.

Okay, I tried this:

void Update () {
		rigidbody2D.velocity = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * Speed;
	}

But the angle is not behaving as I thought it would. “0” sends it straight to the right, and “1” is about 50 degrees from that.
The angles I am working with are the regular 360 degree compass, but they don’t come out that way in the game.

The first set of equations you gave me shows a need to put in an x and y values. That’s what I’m trying to calculate; I don’t have them yet.

I never took trig, so I don’t know what I’m doing here…

Mathf.Cos and Mathf.Sin expects radians, not degrees.

1 radian = 180 / PI = 57.29577…

there’s your “about 50 degrees”

Atan2 also returns radians.

It’s actually kind of annoying, Unity is not consistent with if their method uses radians or degrees. It appears if it’s a trig method, they use radians, and all else use degrees… usually.

You can multiply radians by 180/PI to get the degrees. And you can multiply degrees by PI/180 to get radians. (180 degrees is the same as PI radians… a radian is the arclength of an arc around a unit circle at that rotation… it’s used in place of degrees, because degrees are just splitting a circle into 360 arbitrary ticks. Radians are based on inherent properties of a unit circle)

These values are stored as constants on Mathf.

Mathf.Rad2Deg
Mathf.Deg2Rad

So the code I really want is

rigidbody2D.velocity = new Vector2(Mathf.Cos( Mathf.Deg2Rad *angle), Mathf.Sin(Mathf.Deg2Rad * angle) * startVelocity;

…or to save a few cycles:

angle *= Mathf.Deg2Rad;
		rigidbody2D.velocity = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle) * startVelocity);

EDIT: Fixed compile error

Deg2Rad is a constant, not a method, you multiply by it.

var radAngle = degAngle * Mathf.Deg2Rad;