Search Unity

Spawning a Player Object?

Discussion in 'Multiplayer' started by chrisall76, Jun 30, 2015.

  1. chrisall76

    chrisall76

    Joined:
    May 19, 2012
    Posts:
    667
    I want to go about spawning the player with my own script instead of the NetworkManager. I can't seem to find out how to do this though/how the NetworkManager spawns the player, does anyone know? Here's how I spawned the player before.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4. using System.Collections;
    5.  
    6. public class PlayerSpawn : NetworkBehaviour {
    7.  
    8.     public GameObject MainTrainer;
    9.     public GameObject MainCam;
    10.     public MonsterInfo monInfo;
    11.  
    12.     void Start(){
    13.         CreatePlayer ();
    14.     }
    15.  
    16.     public void CreatePlayer(){
    17.         GameObject Trainer = null;
    18.         Trainer = (GameObject)GameObject.Instantiate (MainTrainer, transform.position, Quaternion.identity);
    19.  
    20.         GameObject Cam = (GameObject)GameObject.Instantiate (MainCam, transform.position, Quaternion.identity);
    21.         Trainer.GetComponent<TPBattle> ().mainCam = Cam.GetComponent<FreeLookCam>();
    22.         Trainer.GetComponent<TPBattle> ().monInfo = monInfo;
    23.         Cam.GetComponent<FreeLookCam> ().SetTarget (Trainer.transform);
    24.     }
    25. }
    26.  
     
  2. chrisall76

    chrisall76

    Joined:
    May 19, 2012
    Posts:
    667
  3. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    I just ran through this myself not 3yrs ago! Turns out, it's pretty easy if you just extend the NetworkManager class. Here's what I did:
    1) Create a script that extends NetworkManager so you have all the functionality it does. Add your override code in there. Mine looks like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class NetworkStuff : NetworkManager
    6. {
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17.  
    18.     public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
    19.     {
    20.         var player = (GameObject)GameObject.Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
    21.        
    22.         GameObject parent = GameObject.Find("Hull");
    23.         Debug.Log("Parent: " + parent.name);
    24.         player.transform.parent = parent.transform;
    25.         NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
    26.     }
    27. }
    28.  
    2) Use your script instead of the default NetworkManager component. One note I ran into that threw me for a bit while testing, is you can't just disable the default NetworkManager on your network stuff empty object. Mine didn't start working properly until I actually removed the original NetworkManager.