- Home /
Is it possible to translate an object diagonally at a specific angle?
I'm working on a 2D game, and I have an object that I would like to move diagonally, at 30°. I do not want to ever rotate the object, I just want it to move in a straight line along the x and y axis'. Ideally, can I transform.Translate(angle converted to a vector3)?
Answer by Owen-Reynolds · Sep 10, 2014 at 03:11 AM
transform.Translate
is mostly a shortcut to easily more your forward, or left or right. Once you start doing fancy stuff, it seems easier to change position directly.
To get a line going 30 degrees from right, for example:
Vector3 A = Quaternion.Euler(0,30,0) * Vector3.right;
The first part assumes you have a top view. It rotates 30 degrees around Y. For a front view, use Z, the 3rd slot (or trial and error.)
Then, to move, just add:
transform.position += A;
Can multiply A by speed, or whatever.
If you want to move 30 degrees from your current facing, the math is the same, but substitute "forward from me" for right:
Vector3 A = Quaternion.Euler(0,30,0)*transform.forward;
This is exactly what I was looking for, and it worked like a charm. thank you!
Answer by Mubanga · Sep 10, 2014 at 03:26 AM
You can definitely translate an angle in to a vector 3 using Mathf.sin
or tan or cos whatever you need, it is just basic geometry.