- Home /
Question by
ManishOtaval · Nov 07, 2019 at 04:10 PM ·
networkingmultiplayerplayerspawn
ReplacePlayerForConnection works but...
I have a phantom player and i want replace it with player prefab selected by user. NetworkServer.ReplacePlayerForConnection works well on server side but not working on client side.
I need phantom player on first place then replace it with user selected player prefab.
I have a custom NetworkManage script named NetNamager. That spawns phantom player. And i want phantom player spawns user selected player and replace it with itself.
What's going wrong?
Here it is my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Phantom : NetworkBehaviour
{
// Player Prefabs
public GameObject[] playerSelect;
//Position & Rotation For New Player
Transform spawnPoint;
// Selected Player Index
public int playerIndex;
void Start()
{
// Assign NetworkStartPosition Randomly
spawnPoint = NetManager.singleton.GetStartPosition();
}
void Update()
{
// Replace character with assigned keyboard F button
if (Input.GetButtonDown("Action"))
{
CmdReplacePlayer();
}
}
[Command]
void CmdReplacePlayer()
{
// Instantiate Selected Player Locally
GameObject player = Instantiate(playerSelect[playerIndex], spawnPoint.position, spawnPoint.rotation);
// Store Connection For Server in Variable
var connectionServer = connectionToClient;
// Store Connection For Client in Variable
var connectionClient = connectionToServer;
if (isServer)
{
print("Replacing Sever Player...");
// Destroy This Player(Phantom Player)
Destroy(gameObject);
// If you are Server spawn & replace character with connectionToClient on server
NetworkServer.Spawn(player);
NetworkServer.ReplacePlayerForConnection(connectionServer, player, playerControllerId);
}
else
{
print("Replacing Client Player...");
// Destroy This Player(Phantom Player)
Destroy(gameObject);
// If you are Client spawn & replace character with connectionToServer on server
NetworkServer.Spawn(player);
NetworkServer.ReplacePlayerForConnection(connectionClient, player, playerControllerId);
}
}
}
Comment
What exactly do you mean by phantom player? Is it a role in your game?