- Home /
Custom Object Visibility (UNet)
I have created a test project to work in and create (learn) ways to do the things that my main project will need. Currently I am working on object visibility (showing/hiding based on proximity).
I have an enemy prefab set up with child transforms to hold one GameObject each, which is also a registered prefab. Each Enemy is randomly generated and comprised of 3 visible parts. Here is the relevant code:
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
// Instantiate prefab.
GameObject player = (GameObject)Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
player.transform.position = startPositions.GetRandom().position;
// Spawn ship parts.
ShipInfo theShip = player.GetComponent<ShipInfo>();
playerShips.Add(theShip);
// Add the player.
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
NetworkInstanceId theShipID = theShip.gameObject.GetComponent<NetworkIdentity>().netId;
// FRONT
GameObject objToSpawn = shipParts.GetRandom();
GameObject newFrontObj = GameObject.Instantiate(objToSpawn);
theShip.SetFrontObj(newFrontObj);
NetworkServer.Spawn(newFrontObj);
NetworkInstanceId frontPartID = newFrontObj.GetComponent<NetworkIdentity>().netId;
ClientScene.objects[frontPartID].transform.parent = ClientScene.objects[theShipID].transform.FindChild("Front").transform;
newFrontObj.transform.localPosition = Vector3.zero;
newFrontObj.transform.rotation = Quaternion.identity;
// BASE
objToSpawn = shipParts.GetRandom();
GameObject newBaseObj = GameObject.Instantiate(objToSpawn);
theShip.SetBaseObj(newBaseObj);
NetworkServer.Spawn(newBaseObj);
NetworkInstanceId basePartID = newBaseObj.GetComponent<NetworkIdentity>().netId;
ClientScene.objects[basePartID].transform.parent = ClientScene.objects[theShipID].transform.FindChild("Base").transform;
newBaseObj.transform.localPosition = Vector3.zero;
newBaseObj.transform.rotation = Quaternion.identity;
// REAR
objToSpawn = shipParts.GetRandom();
GameObject newRearObj = GameObject.Instantiate(objToSpawn);
theShip.SetRearObj(newRearObj);
NetworkServer.Spawn(newRearObj);
NetworkInstanceId rearPartID = newRearObj.GetComponent<NetworkIdentity>().netId;
ClientScene.objects[rearPartID].transform.parent = ClientScene.objects[theShipID].transform.FindChild("Rear").transform;
newRearObj.transform.localPosition = Vector3.zero;
newRearObj.transform.rotation = Quaternion.identity;
Debug.Log("Player added with ID: " + conn.connectionId);
int playerConnectionID = theShip.connectionToClient.connectionId;
if (playerConnectionID == -1)
{
playerConnectionID = 1;
}
else if (playerConnectionID > 0)
{
playerConnectionID++;
}
theShip.CmdSetTagObjectName("Player" + playerConnectionID);
newFrontObj.name = "frontPart";
newFrontObj.GetComponent<ParentTagSystem>()
.CmdSetupObjectName("Player" + playerConnectionID, PartType.Front);
newBaseObj.name = "basePart";
newBaseObj.GetComponent<ParentTagSystem>()
.CmdSetupObjectName("Player" + playerConnectionID, PartType.Base);
newRearObj.name = "rearPart";
newRearObj.GetComponent<ParentTagSystem>()
.CmdSetupObjectName("Player" + playerConnectionID, PartType.Rear);
}
This works fine. The problem is with the proximity checker. When it notices a player is far enough away it destroys the enemy object by default. I'm not sure how to customize this behaviour. I've been looking through the documentation for a while now and I'm not getting it. It is possible I am just way too tired right now so hopefully someone can simply point me in the right direction.
What I probably want to have happen is actual disabling (unless there is a better option) of the objects rather than destroying of them. Alternatively I would have the server somehow tell the client how to reconstruct the correct enemy GameObject. This is probably not that difficult at all, I'm just not seeing how to control it. I imagine this would be a RPC type situation.