- Home /
Player ReSpawn Logic
I was learning multiplayer game implementation through Unity Multiplayer system. So I come across this really good tutorials written for beginners:
Introduction to a Simple Multiplayer Example
From this tutorial, I can't able to understand this page content: Death and Respawning
Through this code, in tutorial they are talking about our player will respawn (player will transform at 0 position and health will be 100) and player can start fighting again.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections;
public class Health : NetworkBehaviour {
public const int maxHealth = 100;
[SyncVar(hook = "OnChangeHealth")]
public int currentHealth = maxHealth;
public RectTransform healthBar;
public void TakeDamage(int amount)
{
if (!isServer)
return;
currentHealth -= amount;
if (currentHealth <= 0)
{
currentHealth = maxHealth;
// called on the Server, but invoked on the Clients
RpcRespawn();
}
}
void OnChangeHealth (int currentHealth )
{
healthBar.sizeDelta = new Vector2(currentHealth , healthBar.sizeDelta.y);
}
[ClientRpc]
void RpcRespawn()
{
if (isLocalPlayer)
{
// move back to zero location
transform.position = Vector3.zero;
}
}
}
As per my thinking -> All clients are executing ClientRPC so all devices local players will move at the spawn position and health get full. As per this tutorial -> only own player's spawn position and health get updated.
So why this thing is happening that I can't able to understand? Actually RPC get called on all clients then all clients should require to move at start position and get full health. Please give me some explanation in this.
Answer by Captain_Pineapple · Jul 06, 2018 at 11:32 AM
Hey there,
as far as i understand this the rpc is indeed called at every client, so you got this point right. But if you take a look at the RpcRespawn()
functions content you will notice that there is an if statement checking if the rpc call is called at the objects owner with if(isLocalPlayer)
. Thus the actual content of the function will only be called at one player even though the function as a whole will be called at every player.
In the end this is ok since the respawn function will probably not be called too often. This way of handling things is not recommended for rpc's that will be called multiple times per second since you will create way too much network traffic.
Hope this helps.
As per my consideration, each device's local player will execute same respawn position code. what do you think ??
No, the FUNCTION will be called at each player. But the variable isLocalPlayer
should only be true on the playerobject of the current actual local player. Thus the functions content in the if-statement should only be called at the local player owner. If you really check this and find that this is not true then please let us know about this.
Yes may be you are right but I will do practical check of this, as per my understanding In my device my player is local player for the game and in my friend's device his player is local player for that game.... So we both have our own local player in each device, what do you think?