Get Position At an Angle

I am working on a 2d game where I need to calculate a position a set distance away from the player at an angle (in degrees). I don’t know how to get the position based on an angle.

any help would be appreciated!

multiple the angle by Mathf.Degree2Radian and use the sine and cosine functions on the x and y. (what goes where depends on how you want to orient the degree and vectors)

2 Likes

I like to just feed the number into a Quaternion and use it to rotate a vector.

If we agree that:

  • zero is straight up (Vector3.up)
  • your angle is in degrees in angle
  • the distance is in distance (world units)
  • whereIAmRightNow is where we are right now,

Then:

Vector3 distantPosition = whereIAmRightNow +
         Quaternion.Euler( 0, 0, angle) * Vector3.up * distance;

If zero is straight right, replace Vector3.up with Vector3.right,

If you are rotating around another angle besides Z (the example above), make adjustments.

etc.

2 Likes

this approach works but the angle is made in up direction only , the unity takes the up direction as origin . How can I get the angle at any desired origin like right.

Is angle relative to player forward direction. Or angle is in world space.
Is it top down 2d or side 2d game ?

1 Like

How will be it’s sol for side 2D game

No, Unity assumes nothing. Vector3 is just a vector, three floating point numbers, nothing more.

I assume Vector3.up in my example code above.

Read the first line for this assumption:

You can substitute any vector for the “zero,” such as Vector3.right if you prefer.

If the angle is going around clockwise and you want counter-clockwise, or vice-versa, then just use -angle instead of angle