- Home /
 
Network - Killing Player, Then Respawn - Issue with RPC
Hello,
I'm starting to get to grips with Unity's networking tools. Pretty awesome stuff... Anyway, I'm working on a kill script and a respawn script, both are very close to working!
If I am my network (so, I'm the dude getting killed):
My Kill script (attached to player) is basically watching out for my health, if it drops below 0, then initiate blood splatter and sends player.transform to spawn script (attached to GO on root).
My Spawn script then turns off the player.gameObject, waits a few seconds, changes the players position to a randomly selected spawn point, turns the gameObject back on, restores health.
My kill script also initiates an RPC, but then is when it goes a little weird... I keep changing my script around, but whats happening right now. I, the player spawns, in a randomly selected point... as do my clients, but they spawn in a different place according to their player. In other words, I die, I spawn top left of map, but the person who killed me (client) sees me spawn bottom right of map.
I think its to do with my randomly selecting spawn point (spawn = Random.Range(0, spawnPoints.length);)
I hope your still with me, here is my HealthSystem (Kill - attached to player):
 function Update(){
 // -- We are dead -- \\
     if(currentHealthPercent < 1 || currentHealth < 1){
         alive = false;
     }
 
     if(alive == false){
         if(networkView.isMine){
                         
             //kill us (turn off gameObject)
             KillPlayer(transform);
             
             //tell clients to turn off gameObject too
             var viewID = Network.AllocateViewID();
             networkView.RPC("SendKillPlayer", RPCMode.All, viewID);
             
             //alive = true;
         }
     }
 }
 function KillPlayer(player : Transform){
     
     print("You Died...");
     
     //blood splatter
     var randomRotate = Random.Range(0,360);
     var clone : Transform;
     clone = Network.Instantiate(bloodSplatter, Vector3(transform.position.x,0.01,transform.position.z), transform.rotation, 0);
     
     //randomly respawn player
     for (var go in FindObjectsOfType(GameObject)){
         go.SendMessage("RespawnPlayer", player, SendMessageOptions.DontRequireReceiver);
         print("Respawning Player...");
     }
 }
 
 
 @RPC
 function SendKillPlayer(viewID : NetworkViewID){
     
     for (var go in FindObjectsOfType(GameObject)){
         go.SendMessage("RespawnPlayer_CLIENT", transform, SendMessageOptions.DontRequireReceiver);
         print("Respawning Player CLIENT...");
     }
 }
 
               And here is my SpawnPrefab Script (Spawn - attached to root hierarchy)
var spawn : int;
 function RespawnPlayer(character : Transform){
     //SET UP RANDOM SPAWN DIGIT
     spawn = Random.Range(0, spawnPoints.length);
     
     //SELF
     RespawnPlayer_SELF(character, spawn);
     
     //CLENTS
     //var viewID = Network.AllocateViewID();
     //networkView.RPC("RespawnPlayer_CLIENT", RPCMode.All, viewID, character);
     
 } 
 function RespawnPlayer_SELF(player : Transform, spawn : int){
     
     print("** Respawn Active! **");
     
     //turn off player << LOCALLY
     player.gameObject.SetActiveRecursively(false);
     
     //wait a few seconds
     yield WaitForSeconds(3);
     
     //randomly respawn player
     player.transform.position = spawnPoints[spawn].transform.position;
     
     print("Reinitiating Player...");
     
     //restore health
     health = player.gameObject.GetComponent(HealthSystem);
     health.currentHealth = health.maxHealth;
     
     //set health controller's alive back to true
     health.alive = true;
 }
 @RPC
 function RespawnPlayer_CLIENT(client : Transform){ //, spawn : int){  viewID : NetworkViewID
 
     print("******* RPC ACTIVE - CLIENT *******");
     
     //turn off player << CLIENT
     client.gameObject.SetActiveRecursively(false);
     
     //wait a few seconds
     yield WaitForSeconds(3); //extra time (incase of lag)
     
     //PLAYER'S SPAWN POINT ALREADY SELECTED, spawn in clients
     client.transform.position = spawnPoints[spawn].transform.position;
     
     //restore health < CLIENT (SHOULD BE EQUAL TO PLAYER)
     health = client.gameObject.GetComponent(HealthSystem);
     health.currentHealth = health.maxHealth;
     
     //turn players renderer on, but deactivate rendererd guns, AND direction arrows < CLIENT
     client.gameObject.SetActiveRecursively(true);
     var weapons = client.gameObject.GetComponent(WeaponController);
     weapons.changeWeapon(weapons.currentWeapon);
     
     var character = client.gameObject.GetComponent(CharacterMove);
     character.playerArrow.gameObject.SetActiveRecursively(false);
     character.enemyArrow.gameObject.SetActiveRecursively(false);
 }
 
               ... Well thats a mouth full. I think, what I need to do, is have the Random value that selects my random spawn point, the same for local and clients, but I have no idea how to pass on those variables with the method I'm using!
Answer by nastasache · Oct 20, 2013 at 10:25 PM
I can't help you too much, I can only share a part of code (mainly, the original source is TPS Unity3D tutorial) taking care about spawning on rand points (and working for me as expected), maybe you can get a bit a direction:
 private NetworkPlayer localPlayer;
 private NetworkViewID localTransformViewID;
 private bool isInstantiated = false;
 
 public void OnPlayerConnected (NetworkPlayer playerNID) {
     NetworkViewID transformViewID = Network.AllocateViewID();
     networkView.RPC("InitPlayer", playerNID, playerNID, transformViewID);
     
 }    
 
 [RPC]
 void InitPlayer (NetworkPlayer playerNID, NetworkViewID transformViewID ) {
     localPlayer = playerNID;
     localTransformViewID = transformViewID;
 }    
 
 void Update() {
 
     if (Network.isClient  && !isInstantiated) {
     
         GameObject[] spawnPointsInScene = GameObject.FindGameObjectsWithTag("SpawnPoint");
         
         int idx = Random.Range(0,spawnPointsInScene.Length);
         
         currentSpawnPoint = spawnPointsInScene[idx].transform.position;
         
         networkView.RPC("SpawnPlayer", RPCMode.AllBuffered, localPlayer, localTransformViewID, currentSpawnPoint);
     
         isInstantiated = true;
     }
 }
 
 [RPC]
 void SpawnPlayer (NetworkPlayer playerNID, NetworkViewID transformViewID, Vector3 currentSpawnPoint) {
 
     instantiatedPlayer = Instantiate(playerPrefab, currentSpawnPoint, transform.rotation) as Transform;
     
 }
 
 
               Probably, depending by your other rules in networking, it's about RPCMode and Network.isClient/Network.isServer (or networkView.isMine) to not take rand() as different on each client when a client ask for instantiating.
Your answer
 
             Follow this Question
Related Questions
Network initializing player 1 Answer
Generate gameObject horde, more positioned towards front of horde 1 Answer
Small RPC Code Not Working 1 Answer
RPC function to update clients varialbe 0 Answers
Mutliplayer SetActive(true) 0 Answers