Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Is command only works on master (host) player? [Code attached]

Discussion in 'Multiplayer' started by Go2ready, Jul 7, 2015.

  1. Go2ready

    Go2ready

    Joined:
    May 8, 2015
    Posts:
    29
    Hello all,

    I am running a shooting game where player have to command server to shoot, the server will execute the shoot, put value into shoot cd as [SyncVar].

    The strange thing is that, only host can command fire, and it is affecting all the other client. So when host do a one click, all the client and the host itself will fire, the other client's click have no effect.

    Here is the code.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Linq;
    3. using Assets.scripts.Library;
    4. using UnityEngine;
    5. using UnityEngine.Networking;
    6. using UnityEngine.Networking.NetworkSystem;
    7.  
    8. namespace Assets.scripts.Controllers
    9. {
    10.     public class PlayerController : NetworkBehaviour
    11.     {
    12.         public GunBarrelController Gbc;
    13.  
    14.         public GranadeLauncherController GlcLeft;
    15.         public GranadeLauncherController GlcRight;
    16.  
    17.         [SyncVar]
    18.         public float Health = 100;
    19.         [SyncVar]
    20.         public float Armor = 100;
    21.         [SyncVar]
    22.         private float fireRate = 5.0f;//Value for bar to track
    23.         [SyncVar]
    24.         private float _nextFire = 0.0f;
    25.  
    26.         ......
    27.  
    28.         void Start()
    29.         {
    30.             this._acceptInput = this.isLocalPlayer;
    31.             if (this._acceptInput) {
    32.                 this._lockedTraget = new LockedTarget ();
    33.                 this._wm = WeaponMode.MainGun;
    34.                 CameraRegistry.activateGroupCamera (new string[]{"Main Camera", "GUI Camera"});
    35.                 this._detectedEnemies = Enumerable.Empty<GameObject> ();
    36.                 this.GetComponentInChildren<GunBarrelController> ().AcceptInput = true;
    37.  
    38.                 Utilities.MakeGameObjectAndItsChildAstag(this.gameObject ,"Player");
    39.                 Utilities.MakeGameObjectAndItsChildAslayer(this.gameObject, "Player");
    40.             } else {
    41.                 Utilities.MakeGameObjectAndItsChildAstag(this.gameObject ,"Enemy");
    42.                 Utilities.MakeGameObjectAndItsChildAslayer(this.gameObject, "Enemy");
    43.             }
    44.             ......
    45.         }
    46.  
    47.         void Update()
    48.         {
    49.             if(this._acceptInput)
    50.             {
    51.                 this.HandleUserInput();
    52.                 ......
    53.             }
    54.             ......
    55.         }
    56.  
    57.         private void HandleUserInput()
    58.         {
    59.             ......
    60.  
    61.             this.CmdUserControlFire();
    62.  
    63.             ......
    64.         }
    65.  
    66. ......
    67.  
    68.         [Command]
    69.         private void CmdUserControlFire()
    70.         {
    71.             if (Input.GetButton("Fire1") &&
    72.                 CameraStateController.CurrentCameraState != CameraStateController.CameraState.Support)
    73.             {
    74.                 if (Time.time > this._nextFire && this._wm == WeaponMode.MainGun)
    75.                 {
    76.                     this._nextFire = Time.time + this.fireRate;
    77.                     this.Gbc.Fire();
    78.                 }
    79.                 else if (Time.time > this._nextFire && this._wm == WeaponMode.Missle)
    80.                 {
    81.                     if (this._lockedTraget.GetLockedStatus() && this._lockedTraget.GetLockedTarget() != null)
    82.                     {
    83.                         this._nextFire = Time.time + this.fireRate;
    84.                         this.Gbc.FireMissle(this._lockedTraget.GetLockedTarget());
    85.                     }
    86.                 }
    87.                 else if (Time.time > this._nextFire && this._wm == WeaponMode.Smoke)
    88.                 {
    89.                     this._nextFire = Time.time + this.fireRate;
    90.                     this.GlcLeft.Fire();
    91.                     this.GlcRight.Fire();
    92.                 }
    93.             }
    94.         }
    95.  
    96. ......
    97.  
    98.     }
    99. }
    100.  
    The Gbc(GunBarrelController) is a as child object on top of player's gun , it is in charge of spawning bullets. It does not have player identity attached. Here is the implementation

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. namespace Assets.scripts.Controllers
    5. {
    6.     public class GunBarrelController : MonoBehaviour {
    7.  
    8.         public GameObject shell;
    9.         public GameObject missle;
    10.         public GameObject explosionParticle;
    11.         public GameObject barrel;
    12.         public float recoil = 1f;
    13.  
    14.         private bool recoilFlag = false;
    15.  
    16.         void FixedUpdate()
    17.         {
    18.             Ray ray = new Ray (this.transform.position, this.transform.forward);
    19.             RaycastHit contact;
    20.      
    21.             if (Physics.Raycast (ray, out contact, Mathf.Infinity, Utilities.CameraLayerMask))
    22.             {
    23.                 //                Debug.Log("Barrel Pointing: " + contact.point);
    24.                 GameObject.Find("HUD_Aim").GetComponent<bl_Hud>().HudInfo.Offset = contact.point;
    25.             }
    26.         }
    27.  
    28.         public void Fire()
    29.         {
    30.             Instantiate(this.shell, this.transform.position, this.transform.rotation);
    31.             NetworkServer.Spawn(this.shell);
    32.             if (this.explosionParticle != null)
    33.             {
    34.                 Instantiate(this.explosionParticle, this.transform.position, this.transform.rotation);
    35.                 NetworkServer.Spawn(this.explosionParticle);
    36.             }
    37.         }
    38.  
    39.         public void FireMissle(GameObject target)
    40.         {
    41.             GameObject missileGo = (GameObject) Instantiate(this.missle, this.transform.position, this.transform.rotation);
    42.             Guided_Missle gm = missileGo.GetComponent<Guided_Missle>();
    43.             if (gm != null)
    44.             {
    45.                 gm.SetTarget(target);
    46.                 NetworkServer.Spawn(missileGo);
    47.             }
    48.             if (this.explosionParticle != null)
    49.             {
    50.                 Instantiate(this.explosionParticle, this.transform.position, this.transform.rotation);
    51.                 NetworkServer.Spawn(this.explosionParticle);
    52.             }
    53.         }
    54.     }
    55. }
    56.  
    They are the same player prefab, spawned by the unmodified network manager. Am I misunderstood command or is there something I am missing here?

    Thank you very much for reading this, any help is greatly appreciated!
     
  2. Go2ready

    Go2ready

    Joined:
    May 8, 2015
    Posts:
    29
    According to unity manual,

    I still don't see what I am doing wrong, does this means that the master player have ownership over all the other player's player object in my case? How can I check that, it is unclear in the manual.
     
  3. Go2ready

    Go2ready

    Joined:
    May 8, 2015
    Posts:
    29
  4. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    use OnStartLocalPlayer() instead of Start()
     
    DylanSemititsky and Go2ready like this.
  5. Go2ready

    Go2ready

    Joined:
    May 8, 2015
    Posts:
    29
    Thank you! That might be it, I never thought about that! Will have a try tonight.;)