I wanted to spawn players manually in my game scene with the exact same spawn position, which is taken from a SpawnPosition object, and a slightly different rotation for every player prefab which gets spawned. I have set my rotations in a List of type Quaternion in my NetworkManager script. In the OnServerAddPlayer() method, I am then calling another method which then Instantiates a new player from the prefab and sets the position (taken from the spawn point) and the custom rotation (which is taken from the list).
The problem is, that it seems like the client who joins, does not rotate at all, even though the rotation is set properly when taking a look in the hierarchy (hosting with the Unity Editor). Why are clients stuck in the default rotation position, and are not applying the rotation? And why is the rotation set properly when viewing the rotation of the client’s player prefab in the hierarchy?
using UnityEngine;
using UnityEngine.Networking;
using System.Collections.Generic;
public class CustomSpawner : NetworkManager
{
public Transform spawnPoint;
private Quaternion spawnRotation;
private int index = 0;
public List<Quaternion> rotations = new List<Quaternion>()
{
Quaternion.Euler(0,20,0), Quaternion.Euler(0, 77.5f, 0), Quaternion.Euler(0, 135, 0)
};
//this function overrides the default behaviour when a client connects to the server and is added to the server
//the 'OnClientConnect' method actually is getting called first and then sends a notification to this method in order to spawn the new client into the scene
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
Debug.Log("Connection established with client " + conn.address);
spawnPlayer(conn, playerControllerId);
if (numPlayers > 0 && numPlayers < 2)
{
index = 1;
}
else if (numPlayers > 1 && numPlayers < 3)
{
index = 2;
}
Debug.Log(numPlayers +"<numPlayers | index >" + index);
}
public override void OnServerDisconnect(NetworkConnection conn)
{
Debug.Log("Client " + conn.address + " disconnected from the server");
NetworkServer.DestroyPlayersForConnection(conn);
}
//This replaces the usage of "Auto Create Player"
/**
* This is called at the beginning whenever connecting to the server. With the 'AddPlayer' method, it sets its player into the scene and sends
* a notification to the server where OnServerAddPlayer methods is waiting for a new connection to instantiate and spawn the player finally.
**/
public override void OnClientConnect(NetworkConnection conn)
{
ClientScene.Ready(conn);
ClientScene.AddPlayer(conn, 0);
}
private void spawnPlayer(NetworkConnection conn, short playerControllerId)
{
spawnRotation = rotations[index];
Debug.Log("Setting player settings");
Debug.Log("Index before spawning: " + index);
//playerPrefab => Unity predefined variable which takes the player prefab set at the network manager GUI.
//it is also possible to do import the prefab manually into the script.
GameObject player = (GameObject) Instantiate(playerPrefab, spawnPoint.position, spawnRotation);
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
}