I'm trying to move a player to a specific spawnpoint when it joins the game. I have client based movement and can't seem to figure out how to teleport them. The host is always in the right position, but the client for some reason always teleports back to (0, 0). I've already tried multiple combinations with ClientRpc, ServerRpc, client transforms, server transforms, coroutines, but nothing seems to work. Since I'm working with 2 different player prefabs I'm also instantiating them manually.
I am very lost
#Teleport player after spawning
1 messages · Page 1 of 1 (latest)
i have this on both playerrpefabs
{
if(!IsLocalPlayer) return;
transform.position = new Vector3(0, 0, 34.54f); //hacker spawn
}
but that only transforms the host to the spawn (which is the wrong player, but thats not the problem)
If you are using client auth tranforms then IsLocalPlayer needs to be true
So this function is correct then no?
As long as the network transform is set to Owner Authority, yea
This is in the ClientNetworkTransform script:
{
return false;
}
I didn't change anything.
public override void OnNetworkSpawn()
{
if (!IsLocalPlayer) return;
if (OwnerClientId == 0) return;
transform.position = new Vector3(0, 0, 34.54f); //hacker spawn
Debug.Log("PlayerNetwork OnNetworkSpawn called for " + OwnerClientId + " at position: " + transform.position);
}
But it's still not working...
The logs tell me the function has been completed, but the transport has done nothing
I also instantiate the prefab on the right position, but it gets teleported to (0, 0) as soon as the client actually connects to it
{
GameObject prefabToSpawn;
Vector3 spawnPos;
Quaternion spawnRot;
Debug.Log("Client connected: " + clientId + ", connected players: " + connectedPlayers);
if (connectedPlayers == 0)
{
prefabToSpawn = robberPrefab;
spawnPos = robberSpawnPoint.position;
spawnRot = robberSpawnPoint.rotation;
}
else
{
prefabToSpawn = hackerPrefab;
spawnPos = hackerSpawnPoint.position;
spawnRot = hackerSpawnPoint.rotation;
}
connectedPlayers++;
GameObject playerInstance = Instantiate(prefabToSpawn, spawnPos, spawnRot);
playerInstance.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
Debug.Log("Spawned player for client: " + clientId + " as " + prefabToSpawn.name + " on this location: " + spawnPos);
}
What version of NGO are you using? the latest versions don't need the separate ClientNetworkTransform anymore.
But as long as the local player is the one changing its position then there shouldn't be any issues
Version 1.8.1, I followed the old codemonkey video so it probably doesn't match up anymore
This is the imported ClientNetworkTransport script in the editor
Opening it just gives me this
namespace Unity.Multiplayer.Samples.Utilities.ClientAuthority
{
/// <summary>
/// Used for syncing a transform with client side changes. This includes host. Pure server as owner isn't supported by this. Please use NetworkTransform
/// for transforms that'll always be owned by the server.
/// </summary>
[DisallowMultipleComponent]
public class ClientNetworkTransform : NetworkTransform
{
/// <summary>
/// Used to determine who can write to this transform. Owner client only.
/// This imposes state to the server. This is putting trust on your clients. Make sure no security-sensitive features use this transform.
/// </summary>
protected override bool OnIsServerAuthoritative()
{
return false;
}
}
}
Could the playerSpawner be the problem?
{
if (!IsServer) return;
// Handle the host's own spawn
HandleClientConnected(NetworkManager.Singleton.LocalClientId);
// Subscribe for future client connections
NetworkManager.OnClientConnectedCallback += HandleClientConnected;
}
private void HandleClientConnected(ulong clientId)
{
GameObject prefabToSpawn;
Vector3 spawnPos;
Quaternion spawnRot;
Debug.Log("Client connected: " + clientId + ", connected players: " + connectedPlayers);
if (connectedPlayers == 0)
{
prefabToSpawn = robberPrefab;
spawnPos = robberSpawnPoint.position;
spawnRot = robberSpawnPoint.rotation;
}
else
{
prefabToSpawn = hackerPrefab;
//get world position from the hacker spawn point
spawnPos = new Vector3(0, 10, 34.54f);
spawnRot = hackerSpawnPoint.rotation;
Debug.Log("1. Hacker Spawn point set");
}
connectedPlayers++;
GameObject playerInstance = Instantiate(prefabToSpawn, spawnPos, spawnRot);
playerInstance.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
Debug.Log("2. Spawned player for client: " + clientId + " as " + prefabToSpawn.name + " on this location: " + spawnPos);
Debug.Log("3. " + playerInstance.name + " spawned at position: " + playerInstance.transform.position + " with rotation: " + playerInstance.transform.rotation);
}```
Yea. The players would have to spawn with server authority for this to work. You could then change it after they players are spawned
So what would I have to change to make it work?
Easiest way would be to update to Unity 6 and NGO2 you can just change the Authority Mode at runtime.
If you can't for whatever reason, then you can change OnIsServerAuthoritative() to return a variable that you can change.