what i mean is a custom way to spawn the player at any time you want being already connected to the server, with a popup in the screen, being able to see the world in action in the background (pretty much like the character selection screen in “CHIVALRY”) so i dont find a way to do that, because everything involves the “OnServerAddPlayer” method, which is triggered automatically when you are succesfully connected to the server and the “auto create player” toggle is checked…
SO what can i do?? maybe it is a noob question but being a long-time unity user (over 8 years using it) i am having a really hard time looking for this kind of info :/, sadly. thanks in advance!
PD: i already have the setup of my “player selection popup” with UI system. just need a way to press one of the buttons and spawn the correct player prefab
Just override OnServerAddPlayer in yout custom NetworkManager;
/// <summary>
/// Add Player Hook;
/// </summary>
/// <param name="conn"></param>
/// <param name="playerControllerId"></param>
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
//wait some time with Coroutine or whatever you want
//Spawning method part here:
GameObject Player = (GameObject)Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, Player, playerControllerId);
}
Well, when you press the button you are called ClientScene.AddPlayer(0), and also you can send a message to server with index of player, it’s better to have an array of prefabs on server and spawn by needs. Try to write a simple NetworkMessage that will tell server which prefab you would like to spawn for your NetworkConnection.
well, i’d do it if know how to haha, am i currently connected i guess? i started the server as “host” so i am connected, right? so no clue how to let know the server(myself) i pressed the UI button
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class LevelManager : NetworkManager {
public static LevelManager instance;
[SerializeField]
public int curElement = 0;
[SerializeField]
public GameObject explosionFX;
public GameObject[] playerPrefabs;
public Transform spawnPosA,spawnPosB;
public GameObject selectionMenu;
GameObject curPlayer;
bool showSelection, isConnected;
// Use this for initialization
void Awake () {
instance = this;
}
// Update is called once per frame
void Update () {
if (isNetworkActive)
{
isConnected = true;
}
else
{
isConnected = false;
}
//-----------------------------------------
showSelection = isConnected;
if (curPlayer)
{
showSelection = false;
}
selectionMenu.SetActive(showSelection);
}
public void SelectElement(int i){
curElement = i;
ClientScene.AddPlayer(0);
}
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
// Debug.Log("trying to spawn...");
Vector3 randomPos = new Vector3(
Random.Range(spawnPosA.position.x,spawnPosB.position.x),
Random.Range(spawnPosA.position.y,spawnPosB.position.y),
Random.Range(spawnPosA.position.z,spawnPosB.position.z)
);
curPlayer = GameObject.Instantiate(playerPrefabs[curElement],randomPos , Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, curPlayer, playerControllerId);
}
}