Search Unity

Vector Math Question

Discussion in 'Scripting' started by John-B, Dec 16, 2010.

  1. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,262
    Math is not my strong suit, so please excuse what I think is a very basic question.

    How do I convert an angle to a vector? I've looked and can't find any function that does this, although I'm not sure what to even call this operation. I need to take an angle, in degrees, and get the corresponding vector.

    See the attached image for a better explanation. I know the vectors that correspond to up (0, 1), down (0, -1), left (-1, 0), and right (1, 0). But I need to calculate the vector for any arbitrary angle. Is there a function for this, or is this a simple interpolation, or a calculation?
     

    Attached Files:

  2. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Vector.X = Cos(angle)
    Vector.Y = Sin(angle)

    (tweak for degrees/radians as needed)

    That should give you the components of a unit vector (magnitude = 1)

    EDIT: assuming that the angle starts at 0 along the positive X axis, then increases counter-clockwise (45 degrees would be up-right, 90 degrees would be straight up, 135 degrees is up-left)
     
    Last edited: Dec 16, 2010