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 /
  • Help Room /
avatar image
0
Question by nopsx · Sep 28, 2015 at 12:45 AM · multiplayer-networkingdamageclient-servercommands

Multiplayer taking damage; commands and clientrpc

Quick question about players taking damage using Unity's HLAPI for multiplayer networking.

My player objects have a method called 'TakeDamage' that accepts the amount of damage and modifies a syncvar on itself called currentHealth.

The players have a box collider in front of them that is activated/deactivated while attacking (melee). While attacking if the player hits something (another player), then it will call 'TakeDamage' on them.

I'm a bit fuzzy on the best practice for this and how I should route the behavior through commands and clientrpc's.

If the player detects a collision with it's melee collider, should it call a command to get the server to call the TakeDamage() method that updates the currentHealth syncvar. Or should the command send an ClientRpc command for the specific player to call TakeDamage on itself?

I know there are many ways of solving the problem, just wondering how others have structured their signal flow.

I'll post some code examples when I make it home to give an idea of what I mean. Thanks for any input in advance!

Edit: Here is an example of what I am doing. I have some more movement and input handling code but this is how I am triggering and taking damage. Am I using commands here correctly? And does the TakeDamage method need to be a ClientRpc call or is making the currentHealth property a syncvar good enough? Thanks again for any help.

 using UnityEngine;
 using UnityEngine.Networking;
 using System.Collections;
 
 [RequireComponent(typeof(CharacterController), typeof(Animator), typeof(NetworkAnimator))]
 public class PlayerCharacter : NetworkBehaviour {
     [SyncVar] public int currentHealth;
 
     public int maxHP = 800;
     public int playerID;
     public BoxCollider basicAtkCollider;        
     public NetworkAnimator networkAnimator;
     
     public void Start() {
         currentHealth = maxHP;
         playerID = Random.Range (1, 1000);
         networkAnimator = GetComponent<NetworkAnimator> ();
     }
 
     public void BasicAttack() {
         basicAtkCollider.enabled = true;
         networkAnimator.SetTrigger ("BasicAttack");
     }
 
     public void TakeDamage(int dmg) {
         currentHealth -= dmg;
         if (currentHealth <= 0) {
             currentHealth = maxHP;
         }
     }
     
     public void OnTriggerEnter(Collider c) {
         if (isLocalPlayer && c.gameObject.tag == "Player") {
             PlayerCharacter enemy = c.gameObject.GetComponent<PlayerCharacter> ();
             if (enemy.playerID != playerID) {
                 int dmg = 34;
                 if (!Network.isServer) {
                     CmdAttackPlayer(enemy.playerID, dmg);
                 } else {
                     InvokePlayerAtk(enemy.playerID, dmg);
                 }
                 basicAtkCollider.enabled = false;
             }
         }
     }
 
 
     [Command]
     public void CmdAttackPlayer(int damagedPlayerID, int dmg) {
         InvokePlayerAtk (damagedPlayerID, dmg);
     }
     
     public void InvokePlayerAtk(int damagedPlayerID, int dmg) {
         PlayerCharacter[] players = FindObjectsOfType (typeof(PlayerCharacter)) as PlayerCharacter[];
         foreach (PlayerCharacter player in players) {
             if (player.playerID == damagedPlayerID) {
                 player.TakeDamage(dmg);
             }
         }
     }
 
 }
 


just want to note that the code generating the player UUID and disabling the box collider is handled better in my codebase, just trying to demonstrate the idea with pseduo-code.

Comment
Add comment · Show 1
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 Kreag · Oct 25, 2015 at 07:38 PM 0
Share

I love this question. I have lots of these types of questions asking about best practices ins$$anonymous$$d of only how to. Did anyone ever get an answer to you about this? or did you figure it out?

0 Replies

· Add your reply
  • Sort: 

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

29 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

Related Questions

local client won't appear and wont move independently 0 Answers

Network.Connect is not working 1 Answer

Cant run command on client? 0 Answers

How to add and track scoring in online multiplayer with UI? (Unet) 0 Answers

Unity Mirror Problem Client Recv: failed to connect to ip=localhost port=7777 reason=System.Net.Sockets.SocketException (0x80004005): Could not resolve host 'localhost' 1 Answer


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