Search Unity

Code works as [Client] but not as [Command]?

Discussion in 'Multiplayer' started by Yogipanda314, Aug 29, 2016.

  1. Yogipanda314

    Yogipanda314

    Joined:
    Aug 28, 2016
    Posts:
    18
    My code works if I change the part to [Client], but I need it to be a [Command] so the server can damage and kill the player, but as command it doesn't work and outputs no errors... Anyone know why this is happening?
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class PlayerShoot : NetworkBehaviour {
    5.  
    6.     private const string PLAYER_TAG = "Player";
    7.  
    8.     public PlayerWeapon weapon;
    9.  
    10. //    public PlayerWeapon weapon;
    11.  
    12.     [SerializeField]
    13.     private Camera cam;
    14.  
    15.     [SerializeField]
    16.     private LayerMask mask;
    17.  
    18.     void Start()
    19.     {
    20.         if (cam == null)
    21.         {
    22.             Debug.LogError ("PlayerShoot: No camera referenced!");
    23.             this.enabled = false;
    24.         }
    25.  
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         if (Input.GetButtonDown ("left click"))
    31.         {
    32.             Debug.Log ("clicking to shoot");
    33.             Shoot ();
    34.         }
    35.     }
    36.  
    37.  
    38.     [Client]
    39.     void Shoot()
    40.     {
    41.         Debug.Log ("Calling shots");
    42.         RaycastHit _hit;
    43.         if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.damage, mask))
    44.         {
    45.             if (_hit.collider.tag == PLAYER_TAG)
    46.             {
    47.                 CmdPlayerShot (_hit.collider.name, weapon.damage);
    48.             }
    49.         }
    50.     }
    51.  
    52.  
    53.     [Command] // This is the part I am talking about, everything up until this point works
    54.     void CmdPlayerShot (string _playerID, int _damage)
    55.     {
    56.             Debug.Log (_playerID + " has been shot.");
    57.  
    58.     Player _player = GameManager.GetPlayer (_playerID);
    59.     _player.RpcTakeDamage(_damage);
    60.     }
    61. }
    62.  
    P.S. I got this code from the Brackeyes FPS Tutorial
     
    Last edited: Aug 30, 2016
  2. Deleted User

    Deleted User

    Guest

    Commands are fired on Server Side Only, RPC - on client side.
     
  3. Yogipanda314

    Yogipanda314

    Joined:
    Aug 28, 2016
    Posts:
    18
    I tried it out, and the code fired, but because it isn't a command the server didn't kill the other player, and nothing he just snapped right back to his position
     
  4. Deleted User

    Deleted User

    Guest

    But I dont see the kill method, only like Damage, and, also, u dont change HP of player on Server, you only do it for client _player.RpcTakeDamage(_damage); use SyncVars.
     
  5. Yogipanda314

    Yogipanda314

    Joined:
    Aug 28, 2016
    Posts:
    18
    I want to thank you, with your help, and the help of a past question, I was able to fix it (at least I think so far). Here is the new code
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class PlayerShoot : NetworkBehaviour {
    5.  
    6.     private const string PLAYER_TAG = "Player";
    7.  
    8.     public PlayerWeapon weapon;
    9.  
    10. //    public PlayerWeapon weapon;
    11.  
    12.     [SerializeField]
    13.     private Camera cam;
    14.  
    15.     [SerializeField]
    16.     private LayerMask mask;
    17.  
    18.     void Start()
    19.     {
    20.         if (cam == null)
    21.         {
    22.             Debug.LogError ("PlayerShoot: No camera referenced!");
    23.             this.enabled = false;
    24.         }
    25.  
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         if (Input.GetButtonDown ("left click"))
    31.         {
    32.             Shoot ();
    33.         }
    34.     }
    35.  
    36.  
    37.     [Client]
    38.     void Shoot()
    39.     {
    40.         RaycastHit _hit;
    41.         if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.damage, mask))
    42.         {
    43.             if (_hit.collider.tag == PLAYER_TAG)
    44.             {
    45.                 CmdServerCallRpc (_hit.collider.name, weapon.damage);
    46. //                PlayerShot (_hit.collider.name, weapon.damage);
    47.             }
    48.         }
    49.     }
    50.  
    51.  
    52.     [Command]
    53.     void CmdServerCallRpc (string _playerID, int _damage)
    54.     {
    55.         RpcServerPlayerShot (_playerID,_damage);
    56.     }
    57.  
    58.     [ClientRpc]
    59.     void RpcServerPlayerShot (string _playerID, int _damage)
    60.     {
    61.             Debug.Log (_playerID + " has been shot.");
    62.  
    63.             Player _player = GameManager.GetPlayer (_playerID);
    64.             _player.RpcTakeDamage (_damage);
    65.     }
    66. }
    67.  
     
  6. Deleted User

    Deleted User

    Guest

    You are welcome)