Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Angle to vector?

Discussion in 'Scripting' started by Marscaleb, Nov 15, 2020.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    987
    This is pretty basic, and I think I've done it before, but I'm just drawing a blank today.

    I have an angle. (And a speed.) I want to convert that into a vector so I can apply it to a rigidbody. (Across the X and Z axis.) How do I do this?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You need to decide what that angle is relative to. So basically get a vector that is your starting direction, as well as an axis of rotation. Then finally you can get the result vector with
    Code (CSharp):
    1. // plug your rotation and axis of rotation into here:
    2. Quaternion myRotation = Quaternion.AngleAxis(myAngle, myRotationAxis);
    3. Vector3 startingDirection = <some starting direction>;
    4. Vector3 result = myRotation * startingDirection;
    If your rotation axis is one of the three "world" x, y, or z axes, you can also use Quaternion.Euler.
     
  3. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    987
    This looks more complicated than I expected, and I don't understand why I need to multiply the rotation by another vector. What kind of vector would I need for a starting direction?

    Just to be clear, I'm not trying to rotate my object, I want apply a velocity. I want to move it in a direction according to an angle (and speed) that I have.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    You don't. I'm multiplying the starting direction by the rotation quaternion, which results in another vector that is the rotated version of the first vector.
    Whatever direction you want a rotation relative to. For example maybe it's the
    transform.forward
    direction of your character or something.
    but an angle relative to what, and rotated on what axis? For example, maybe it's 5 degrees rotated above the horizon. Or 20 degrees to the left of the direction your character is facing.
     
    orionsyndrome likes this.
  5. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    987
    This is definitely more complicated than I thought it would be. I could have sworn it was some method in Vector3 that made a simple one line conversion.

    Well, I managed to get it working. It took a bit of trial and error and some fixing of bugs I didn't know I had in my code to find that angle, but I got it working.
    Thank you.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    orionsyndrome and Kurt-Dekker like this.
  7. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    The most basic form is
    Vector3 vecForAng = angle*Vector3.forward;
    . angle is a quaternion representing a world-coords angle. Unity considers angle (0,0,0) as aimed along +z; that's why we use Vector3.forward. As an example, transform.forward is a shortcut for transform.rotation*Vector3.forward.
     
  8. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    I usually use something like

    return new Vector3(Mathf.Sin(Mathf.deg2rad * angle), 0, Mathf.Cos(Mathf.deg2rad * angle));

    Naturally this only works in 2 dimensions, but it might give you a good idea about what you can do with the third dimension.
     
  9. iogbwoghh

    iogbwoghh

    Joined:
    May 8, 2021
    Posts:
    2
  10. tokegameart

    tokegameart

    Joined:
    Feb 3, 2016
    Posts:
    9
    x = sin(angle)
    y = cos(angle)

    "x" and "y" give the result between 0 to 1

    hope this help
     
  11. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Please do not necro-post.
    Also this is only valid in 2D. It is vastly different in 3D, which is the key point here.

    Incorrect. "x" and "y" give the result between -1 and 1, because that's how sine and cosine work.

    Edit:
    That said, the solution to the problem
    Assuming that the angle is in degrees, and speed is the magnitude:

    In 2D
    Code (csharp):
    1. var rads = angle * Mathf.Deg2Rad;
    2. Vector2 vec = speed * new Vector2(Mathf.Cos(rads), Mathf.Sin(rads));
    In 3D more information is needed, for example a surface normal.
    Code (csharp):
    1. Vector3 vec = Quaternion.AngleAxis(angle, normal) * (speed * Vector3.right);
    A surface normal for the XZ plane is Vector3.up.

    Or, at least for the base planes, you can lock the computation in 2D, then rotate the result orthogonally.
    Code (csharp):
    1. Vector2 v2 = ... // 2D computation from above
    2. Vector3 v3 = new Vector3(v2.x, 0f, v2.y);
     
    Last edited: Sep 23, 2023
    Nad_B and Bunny83 like this.