0
\$\begingroup\$

Im trying to get a directional vector from an euler angle. But im not getting how to do it. This is what i got so far.

escapeAngle += 90;
var radians = escapeAngle * (Mathf.PI / 180);

var escapeAngleAsDirectionVector = new Vector2 ();
escapeAngleAsDirectionVector.x = Mathf.Cos (radians);
escapeAngleAsDirectionVector.y = Mathf.Sin (radians);

escapeAngleAsDirectionVector = transform.TransformPoint (escapeAngleAsDirectionVector);
\$\endgroup\$
  • 1
    \$\begingroup\$ What do you get? What did you expect to get? \$\endgroup\$ – occulus Apr 16 '14 at 13:52
  • \$\begingroup\$ I dont know what to expect more than a vector2 that Points in the direction of the angle. What i get is direction vector pointing in weird angles that is not the angle i provided. \$\endgroup\$ – Daarwin Apr 16 '14 at 14:59
  • \$\begingroup\$ Then edit your question and put that in your question please! \$\endgroup\$ – occulus Apr 16 '14 at 15:16
  • \$\begingroup\$ Can you give a few examples of what you're putting in and what you are getting as a result? First, you are adding 90 degrees so you are supposed to get a perpendicular vector of course. \$\endgroup\$ – wolfdawn Apr 17 '14 at 4:19
  • \$\begingroup\$ First thing is you shouldn't be adding 90 to the angle, unless you want a perpendicular vector rather than one facing in the same direction. \$\endgroup\$ – CoffeeandCode Apr 17 '14 at 13:06
3
\$\begingroup\$

You can create a rotation quaternion from your Euler angles. By multiplying it with a vector 'right', you get a Vector2:

Vector2 v = Quaternion.Euler(x, y, z) * Vector2.right
\$\endgroup\$
  • \$\begingroup\$ also most probably, z = watever, like 0, should yield the same results. you need only 2 angles for a direction. \$\endgroup\$ – v.oddou Oct 15 '14 at 1:55
0
\$\begingroup\$

The correct formula is V = {r*cos(theta), y*sin(theta)} to get a vector from an angle with line at angle theta of length r, so your calculations look right. BUT one thing you need to account for is if the angle is negative. then you would have to add 360 to the angle.

E.g.

if(esacapeAngle < 0)
    escapeAngle += 360;

Another thing you should probably check if the angle is greater than or equal to 360 after the addition, because that wouldn't be valid either.

if(escapeAngle >= 360)
    throw error

then do the rest of your calculations.

This will give you a perpendicular vector though, as you add 90 to the angle.

Converting Polar coordinates to Cartesian coordinates

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.