Search Unity

Creating a random direction vector

Discussion in 'Scripting' started by guitarxe, Jan 2, 2014.

Thread Status:
Not open for further replies.
  1. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
    In a 2D game, I would like to apply force to an object along a random vector. What is the simplest way of doing this?
    I could get a random number between 0 and 360 and convert that to a vector, but there's probably an easier way of doing this?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1. direction = Random.insideUnitCircle.normalized;
    --Eric
     
  3. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
    Perfect, thanks!
     
Thread Status:
Not open for further replies.