Search Unity

Randomise the position of the player

Discussion in 'Scripting' started by missy_pooh, Mar 13, 2012.

  1. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    I would like to ask how to randomise the position of the player once the network is loaded. The code snippet below is what i have done to randomise the position of the player but it doesn't seem to work. All the players are spawn at the same position. Can i know what wrong with the code?

    Code (csharp):
    1.  
    2. function OnNetworkLoadedLevel()
    3. {
    4.     // Randomize starting location
    5.     var spawnpoints : GameObject[] = GameObject.FindGameObjectsWithTag ("Spawnpoint");
    6.     Debug.Log("spawns: "+spawnpoints.length);
    7.    
    8.     var spawnpoint : Transform = spawnpoints[Random.Range(0, spawnpoints.length)].transform;
    9.     var newTrans : Transform = Network.Instantiate(playerPrefab,spawnpoint.position, spawnpoint.rotation, 0);
    10. }
    11.  
    12.  
     
    Last edited: Mar 14, 2012
  2. pjo

    pjo

    Joined:
    Feb 1, 2012
    Posts:
    38
    I would assume that you want to have several spawning points because you don't want a player to spawn in a volcano, in the middle of the ocean or some other bad place to be in. You could save the coordinates of several spots in an array then randomly select from those by generating a random number and mod it with the table size. Then you can instantiate the game object(player) and use those coordinates as the location. Hope this helps.
     
  3. Posly

    Posly

    Joined:
    Jan 14, 2012
    Posts:
    151
    "Random.Range" is exactly what your looking for, you give it a min value and a max value and it randomly picks a value in between. You can use it in two ways. You can set the position of a player randomly in an area, or you can assign specific points where you would like to spawn the player.

    For a large random area it would look something like this:
    Code (csharp):
    1.  
    2. var min : float;
    3. var max : float;
    4.  
    5. function Start () {
    6.  
    7. transform.position = Vector3(Random.Range(min, max), Random.Range(min, max), Random.Range(min, max));
    8.  
    9. }
    10.  
    That was just an example, you can use more variables for the y value so your player doesn't spawn in the heavens.

    Second way: You can use a switch statement

    Code (csharp):
    1.  
    2. var pos1 : Transform;
    3. var pos2 : Transform;
    4. var pos3 : Transform;
    5. var randomNum : int;
    6. var amountOfPos : int;
    7.  
    8. function Start () {
    9.  
    10. randomNum = Random.Range(1, amountOfPos);
    11.  
    12. switch (randomNum) {
    13.  
    14. case 1:
    15. transform.position = pos1;
    16. break;
    17.  
    18. case 2:
    19. transform.position = pos2;
    20. break;
    21.  
    22. case 3;
    23. transform.position = pos3;
    24. break;
    25.  
    26. }
    27.  
    28. }
    29.  
    You could use an array but I never used one before. I hope this helps! Have any question you can PM me, here's the link to random.range.

    http://unity3d.com/support/documentation/ScriptReference/Random.Range.html
     
  4. Chebsi

    Chebsi

    Joined:
    Sep 6, 2011
    Posts:
    24
    pjo is right. Your code doesn't randomize the spawn points, it's only getting an array (basically a list) of the spawn point game objects in your scene.
    You could change it to something like this (might need tweaking for C# since I'm used to C#):
    Code (csharp):
    1.  
    2.     function OnNetworkLoadedLevel()
    3.         {
    4.             // Randomize starting location
    5.             var spawnpoints : GameObject[] = GameObject.FindGameObjectsWithTag ("Spawnpoint");
    6.    
    7.              var spawnpoint : GameObject = spawnpoints[Random.Range(0, spawnpoints.Length)];
    8.  
    9.         }
    10.  
    11.  
    You probably want to initialize the spawnpoint outside of the function though.
     
  5. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    Hello, I only have one spawn point. that wise every player spawn at the same point. my bad. Perhaps you mind to elaborate more on the
    "ou could save the coordinates of several spots in an array then randomly select from those by generating a random number and mod it with the table size. Then you can instantiate the game object(player) and use those coordinates as the location"?
     
  6. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    Hi, the code you wrote is exactly what i have now. what you mean by initialize the spawnpoint outside the function?
     
  7. Chebsi

    Chebsi

    Joined:
    Sep 6, 2011
    Posts:
    24
    What gameobject is this script attached to? It's difficult to help without more details but I'll try.

    If you only have this one game object spawning all your players, then it's possible you are using the same spawnpoint variable (without changing it) for all of your players. So, if you are using Random.Range to get a spawnpoint (let's say it's at point (4,5,0) ) then you instantiate player 1 there. But now you spawnpoint is still (4,5,0). You didn't randomize it again. So if you instantiate another player now they will also spawn at (4,5,0). To fix this, before spawning another player you should make spawnpoint equal another random spawn point by doing this again:

    Code (csharp):
    1. spawnpoint = spawnpoints[Random.Range(0, spawnpoints.Length)];
    This will get a random spawn point from the spawnpoints array and assign it to the spawnpoint variable.

    On the other hand, if this script is on each player gameobject, then there are some other things you should check. Be sure that you have more than one spawn point in your spawnpoint array. Also be sure that those spawnpoints all have different positions. You can do this by adding more debug logs or by looking at the contents of the array in the inspector at runtime.
     
  8. pjo

    pjo

    Joined:
    Feb 1, 2012
    Posts:
    38
    If you want to randomly spawn a players you could use a random range, but that will give you random points and you don't know where your character will spawn. To handle that situation you can go throughout the map and find locations where you would like your character to spawn and save them in a table. Then use "random" to generate a number then mod it with the table size, so the random number is in the range of the table. Then you can go to that table index(random number) and use those coordinates as your spawn points. You don't want to just go ahead and use random coordinates because based on the terrain you could end up spawning in a hole or even worse inside an object and then the player is stuck.
     
  9. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    Hi, my script is attached to an empty gameObject. I try using your method but my player fall infinitely. I don't wrong what wrong? Any ideas?

    spawnpoint = spawnpoints[Random.Range(0, spawnpoints.Length)]; -> got error

    Assets/Scripts/MultiPlayerScripts/GameSetup.js(149,32): BCE0022: Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'.
    Code (csharp):
    1.  
    2. function OnNetworkLoadedLevel()
    3. {
    4.     // Randomize starting location
    5.     var spawnpoints : GameObject[] = GameObject.FindGameObjectsWithTag ("Spawnpoint");
    6.     Debug.Log("spawns: "+spawnpoints.length);
    7.    
    8.     //var spawnpoint : Transform = spawnpoints[Random.Range(0, spawnpoints.length)].transform;
    9.     var spawnpoint : Transform;
    10.     spawnpoint =spawnpoints[Random.Range(0, spawnpoints.Length)];
    11.     var newTrans : Transform = Network.Instantiate(playerPrefab,spawnpoint.position, spawnpoint.rotation, 0);
    12. }
    13.  
    14.  
     
    Last edited: Mar 14, 2012
  10. Chebsi

    Chebsi

    Joined:
    Sep 6, 2011
    Posts:
    24
    Code (csharp):
    1. spawnpoint = spawnpoints[Random.Range(0, spawnpoints.Length)]; -> got error
    You're getting that error because spawnpoint is an object of type Transform but spawnpoints is an array of objects with type GameObject. You're trying to put a GameObject in a variable expecting a Transform. You'd need to get the transform of the GameObjects and assign that to the spawnpoint instead.

    Code (csharp):
    1. spawnpoint = spawnpoints[Random.Range(0, spawnpoints.Length)].transform;
    Next, you said you just attached this script to an empty object in your scene. If it's only one player (the "playerPrefab") then that's ok. But if you have multiple players then you need to do a random spawn point again for each one.
     
  11. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150

    If it's only one player (the "playerPrefab") then that's ok. But if you have multiple players then you need to do a random spawn point again for each one -> Pardon me, but i don't really get what you trying to imply here. I need multiplayer in the scene thatwise i am doing a spawn.
     
  12. kral

    kral

    Joined:
    Aug 7, 2011
    Posts:
    25
    Code (csharp):
    1.  
    2. var spawnpoints : GameObject[];
    3. var playerPrefab : Transform;
    4.  
    5. function OnNetworkLoadedLevel()
    6.  
    7. {
    8.  
    9.     // Randomize starting location
    10.  
    11.    spawnpoints  = GameObject.FindGameObjectsWithTag ("Spawnpoint");
    12.  
    13.     Debug.Log("spawns: "+spawnpoints.length);
    14.  
    15.    
    16.  
    17.     var randomspawnpoint : GameObject = spawnpoints[Random.Range(0, spawnpoints.length)];
    18.  
    19.    Network.Instantiate(playerPrefab,randomspawnpoint.transform.position, randomspawnpoint.transform.rotation, 0);
    20.  
    21. }
    22.  
    23.  
     
  13. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    Hi, it is not working properly at all. My player drop infinitely from the screen.