Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Player's random position

Discussion in 'Scripting' started by H3d_trinity, Apr 6, 2011.

  1. H3d_trinity

    H3d_trinity

    Joined:
    Apr 6, 2011
    Posts:
    6
    Hello;
    I'm doing a game and I want to create a code that allows me that every time the player's health = 0 and when the player loses and begins to play the game again that the player's position changes.
    Thank you for your help:)
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Well, you could use a Random.InsideUnitCircle and then do a Physics.Linecast (linecast is easier than raycast) say from 5000 units above the game environment to 5000 below. This gives you a random point to start at.

    If you are talking random spawnpoints, that is handled differently, you would have an array of empty game objects and do:

    Code (csharp):
    1.  
    2. var mySpawnPoint=spawnPoints[Random.Value * spawnPoints.length];
    3.