- Home /
Converting direction to Vector2
I have an angle (direction) and I need to move a rectangle in that direction. This is the code I have so far where pos
is a rectangle and dir
is the direction:
pos.x += Time.deltaTime * 500 * Mathf.Cos(dir);
pos.y += Time.deltaTime * 500 * Mathf.Sin(dir);
When I run it the object goes off in a random direction!
Edit:
I guess this was not clear: dir
is a float and pos
is a Rect.
Answer by Peter G · Mar 28, 2011 at 12:04 AM
It's worth a mention that dir (your angle) should be in radians.
$$anonymous$$y code worked fine after converting dir
to radians
Answer by DaveA · Mar 27, 2011 at 11:06 PM
It's not random, it's Trigonometric. I don't know if you need that, so try this and see if it's more like what you want:
var p : Vector3;
function Start() p.x = pos.x; p.y = pos.y; p.z = 0;
function Update()
p += Time.deltaTime * 500 * dir;
pos.x = p.x;
pos.y = p.y;
ah yes, sorry, most of the time I see pos, it's position Vector3. Altered code (above)