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.
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class PlayerSpawn : NetworkBehaviour {
public GameObject MainTrainer;
public GameObject MainCam;
public MonsterInfo monInfo;
void Start(){
CreatePlayer ();
}
public void CreatePlayer(){
GameObject Trainer = null;
Trainer = (GameObject)GameObject.Instantiate (MainTrainer, transform.position, Quaternion.identity);
GameObject Cam = (GameObject)GameObject.Instantiate (MainCam, transform.position, Quaternion.identity);
Trainer.GetComponent<TPBattle> ().mainCam = Cam.GetComponent<FreeLookCam>();
Trainer.GetComponent<TPBattle> ().monInfo = monInfo;
Cam.GetComponent<FreeLookCam> ().SetTarget (Trainer.transform);
}
}
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:
- Create a script that extends NetworkManager so you have all the functionality it does. Add your override code in there. Mine looks like this:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class NetworkStuff : NetworkManager
{
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
var player = (GameObject)GameObject.Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
GameObject parent = GameObject.Find("Hull");
Debug.Log("Parent: " + parent.name);
player.transform.parent = parent.transform;
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
}
- 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.