Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by habsfan50 · Feb 09, 2018 at 10:11 PM · networkingclient-server

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:

https://unity3d.com/learn/tutorials/topics/multiplayer-networking/death-and-respawning?playlist=29690

 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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image habsfan50 · Feb 10, 2018 at 02:33 AM 0
Share

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?

@spidermancy612

avatar image Bunny83 habsfan50 · Feb 10, 2018 at 05:43 AM 0
Share

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.

avatar image habsfan50 · Feb 10, 2018 at 07:05 AM 0
Share

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.

avatar image Bunny83 habsfan50 · Feb 10, 2018 at 01:39 PM 1
Share

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.

avatar image habsfan50 Bunny83 · Feb 10, 2018 at 03:12 PM 0
Share

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)?

Show more comments
avatar image Bunny83 habsfan50 · Feb 10, 2018 at 01:40 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

113 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges