Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

spawn in multiplayer not randomly

Discussion in 'Scripting' started by aaronrotc2014, Feb 18, 2015.

  1. aaronrotc2014

    aaronrotc2014

    Joined:
    Apr 17, 2014
    Posts:
    7
    how do i get two players to spawn at different points of the map, i all ready have the spawn points created but they spawn randomly, we don need that
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Spawn : MonoBehaviour
    6. {
    7.     public GameObject player;
    8.     public Transform[] spawnPoints;
    9.     public Camera spawnCam;
    10.     public AudioListener spawnListener;
    11.     public bool spawned = false;
    12.    
    13.     private int curMenu = 0;
    14.    
    15.     private string[] settings;
    16.     private int curIndex;
    17.    
    18.     void Start()
    19.     {
    20.  
    21.         settings = QualitySettings.names;
    22.     }
    23.    
    24.    
    25.     void Update()
    26.     {
    27.         if (spawned) if (Input.GetKeyDown(KeyCode.Escape)) Screen.lockCursor = !Screen.lockCursor;
    28.     }
    29.    
    30.     void OnGUI()
    31.     {
    32.         if (Network.isServer || Network.isClient)
    33.         {
    34.             if (curMenu == 0)
    35.             {
    36.                 if (!spawned)
    37.                 {
    38.                     if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 80, 200, 50), "Spawn"))
    39.                     {
    40.                         SpawnPlayer();
    41.  
    42.                     }
    43.                     if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 25, 200, 50), "Options"))
    44.                     {
    45.                         curMenu = 1;
    46.                     }
    47.                     if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 200, 50), "Quit"))
    48.                     {
    49.                         Application.Quit();
    50.                     }
    51.                 }
    52.                 else
    53.                 {
    54.                     if (!Screen.lockCursor)
    55.                     {
    56.                         if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 80, 200, 50), "Resume"))
    57.                         {
    58.                             Screen.lockCursor = true;
    59.                         }
    60.                         if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 25, 200, 50), "Options"))
    61.                         {
    62.                             curMenu = 1;
    63.                         }
    64.                         if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 200, 50), "Restart"))
    65.                         {
    66.                             Destroy (GameObject.FindWithTag("Player"));
    67.                             Destroy (GameObject.FindWithTag("Player"));
    68.                             Application.LoadLevel(Application.loadedLevel);
    69.                             Screen.lockCursor = false;
    70.                         }
    71.                     }
    72.                 }
    73.             }
    74.             else if (curMenu == 1)
    75.             {
    76.                 if (!spawned || !Screen.lockCursor)
    77.                 {
    78.                     if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 80, 200, 50), "Audio"))
    79.                     {
    80.                         curMenu = 2;
    81.                     }
    82.                     if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 25, 200, 50), "Graphics"))
    83.                     {
    84.                         curMenu = 3;
    85.                     }
    86.                     if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 200, 50), "Back"))
    87.                     {
    88.                         curMenu = 0;
    89.                     }
    90.                 }
    91.             }
    92.             else if (curMenu == 2)
    93.             {
    94.                 AudioListener.volume = GUI.HorizontalSlider(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 25, 200, 50), AudioListener.volume, 0f, 1f);
    95.                 GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 45, 200, 50), "Volume: " + (AudioListener.volume * 100).ToString("F0") + " %");
    96.                 if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 200, 50), "Back"))
    97.                 {
    98.                     curMenu = 0;
    99.                 }
    100.             }
    101.             else if (curMenu == 3)
    102.             {
    103.                 if (GUI.Button(new Rect(Screen.width / 2 - 75, Screen.height / 2 - 25, 150, 50), settings[curIndex]))
    104.                 {
    105.                     QualitySettings.SetQualityLevel(curIndex);
    106.                 }
    107.                 if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 - 25, 20, 50), "←"))
    108.                 {
    109.                     if (curIndex != 0) curIndex--;
    110.                     else curIndex = settings.Length - 1;
    111.                 }
    112.                
    113.                 if (GUI.Button(new Rect(Screen.width / 2 + 80, Screen.height / 2 - 25, 20, 50), "→"))
    114.                 {
    115.                     if (curIndex != settings.Length - 1) curIndex++;
    116.                     else curIndex = 0;
    117.                 }
    118.                
    119.                 //I only made switching between quality settings. You can make it much more complex if you want.
    120.                 if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 200, 50), "Back"))
    121.                 {
    122.                     curMenu = 1;
    123.                 }
    124.             }
    125.         }
    126.     }
    127.    
    128.     void SpawnPlayer()
    129.     {
    130.         spawned = true;
    131.         int random = Random.Range(0, spawnPoints.Length);
    132.         Network.Instantiate(player, spawnPoints[random].position, spawnPoints[random].rotation, 0);
    133.         spawned = true;
    134.         spawned = true;
    135.         Screen.lockCursor = true;
    136.     }
    137.    
    138.     public void Die()
    139.     {
    140.         Screen.lockCursor = false;
    141.         spawned = false;
    142.         spawnCam.enabled = true;
    143.         spawnListener.enabled = true;
    144.     }
    145. }
    146.  
     
  2. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Replace "int random" with a class variable that increments for each spawn. When it reaches the max + 1 number of spawn points, set it to zero.
     
  3. aaronrotc2014

    aaronrotc2014

    Joined:
    Apr 17, 2014
    Posts:
    7
    what is var in C#
     
  4. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    What do you mean?
     
  5. aaronrotc2014

    aaronrotc2014

    Joined:
    Apr 17, 2014
    Posts:
    7
    nevermind i got it, thank you for your help
     
  6. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328