- Home /
Need help understanding ClientRpc from Unity Networking tutorial,Question about clientrpc
Hello, I am going through the Unity networking tutorial and wondering if someone could explain how the ClientRPC function works in the code below? From what I understand when a player object takes damage, on the server the player object's health is reduced. When the health is <= 0, then the server executes the RpcRespawn function on all the clients. The part that confuses me is the " if(isLocalPlayer) " part. On every client's computer who is playing this game, wouldn't this function execute and because each client's player object is the local player on their computer, the why doesn't each client's local player object move to Vector.zero?
Source is here:
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();
}
}
[ClientRpc]
void RpcRespawn()
{
if (isLocalPlayer)
{
// move back to zero location
transform.position = Vector3.zero;
}
}
Thanks in advance.
Answer by spidermancy612 · Feb 09, 2018 at 10:18 PM
isLocalPlayer makes sure the code is only run if you are the version that is on the computer. Everyone is running copies of the same code, so isLocalPlayer allows you to say that you only run the code in the right situation.
Hi, so if you have 2 human players playing on two separate computers, then doesn't RpcRespawn() run on both computers? If so, then what is stopping both human players from returning true when running the code if( isLocalPlayer) part?
isLocalPlayer is a property of the NetworkBehaviour class. It's only true for the playerobject that belongs to the local player. If you have PC1 and PC2. Player A is on PC1 and Player B is on PC2. On every computer / peer both players are represented by an object.So we have:
PC1_A // isLocalPlayer == true
PC1_B // isLocalPlayer == false
PC2_A // isLocalPlayer == false
PC2_B // isLocalPlayer == true
When you call a client RPC on object A it will run on all clients on the A object. However only one client can have isLocalPlayer set to true.
Hi @Bunny83,
When clientRPC is called, I thought it is called on all the clients. So client 1 is PC1 and client 2 is PC2. So when clientRPC is called the RpcRespawn() function will run on PC1 player 1's object (or A) and PC1 player 2's object (or B). Since PC1 player 1's object will return true when running 'isLocalPlayer", this object is moved to (0,0,0). The NetworkTransform would move Player 1's object (A) on PC2 to (0,0,0) as well. At the same time, since PC2 is a client, player 2's object (B) will return true when running 'isLocalPlayer' and move this object to (0,0,0). Then network transform would move this object on PC1 to (0,0,0). In the end both objects end up at (0,0,0). I know this is wrong from watching the tutorials on youtube, but can you explain where I go wrong in my thinking?
Thanks.
RPCs run on the object where the method is attached to on all clients. So when calling an RPC method on object A it will run on all clients on their object A representation. Object B might have the same script but the RPC was called on A only so it runs on A only.
Thanks @Bunny83. Just to make sure I fully understand, if RPC is called on Object A it will run the RPC function on all of the clients (PC1, PC2, ...,n) but just on Object A, and not (Object B,.., Object n). Then in the RpcRespawn() function because Object A is the local player on PC1, (isLocalPlayer) is true for PC1, but (isLocalPlayer) is false for Object A on (PC2, ..., n)?
please do not post answers when you don't answer your own question. There's an add comment / reply button below each post.
Your answer
Follow this Question
Related Questions
Commands and ClientRPC are not running when called 0 Answers
Can someone guide me how to get started with unity to make a multiplayer networking game ? 1 Answer
NullReference Exception FindLocalObject C# 1 Answer
Client with authority can't send information to server 0 Answers
How to avoid spawn delay on client? 0 Answers