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 siddharth3322 · Jul 05, 2018 at 06:19 PM · networkingmultiplayermultiplayer-networkingnetworkplayer

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.

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 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.

Comment
Add comment · Show 3 · 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 siddharth3322 · Jul 06, 2018 at 04:17 PM 0
Share

As per my consideration, each device's local player will execute same respawn position code. what do you think ??

avatar image Captain_Pineapple siddharth3322 · Jul 06, 2018 at 06:30 PM 0
Share

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.

avatar image siddharth3322 Captain_Pineapple · Jul 07, 2018 at 11:34 AM 0
Share

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?

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

174 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 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

Remove players and close server in Unity Multiplayer Game 0 Answers

Unity networking tutorial? 6 Answers

Setup Custom Multiplayer Joining 0 Answers

After 10 Seconds Player Spawning in Multiplayer 0 Answers

Check when a player finished spawning [Netcode For GameObjects!] 2 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