Search Unity

Random free position

Discussion in '2D' started by tito100, Oct 22, 2016.

  1. tito100

    tito100

    Joined:
    Oct 14, 2016
    Posts:
    22
    Hi all.

    I made top town 2d game.
    when room start, or when player die i need find random free position for player.

    colision.png
    where is red collision polygon, there is not free place.
    Also player has too collision, so when I find random position, then player collision circle also cant collide with red polygon

    how I can do some like this?

    Thanks
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    You could generate a position slightly above the map randomly with a random valid rotation, then perform a Rigidbody.SweepTest() straight down to the floor to see if there are any blocking colliders. If there isn't, use that spot. If there is, generate a new position/rotation. Not the most efficient in all situations, but it's an OK method all-around.
     
    tito100 likes this.