Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[WeaponSystem] - Can't change weapons and instantiated bullets on clients

Discussion in 'Multiplayer' started by Nosfera, Mar 15, 2016.

  1. Nosfera


    Sep 26, 2014
    I'm working on my final college project, it is a fps multiplayer and I made a very simple weapon system that works perfectly locally (for my own purposes). However when trying to convert to multiplayer I can't fix some issues. The first one and most basic in my opinion is I can't switch weapon in all clients. I've tried to use RpcClient and Commant but still no results. Basically, I have a class in the weapon that holds its attribute, including the projectile. From another class (WeaponManager), I set the activeWeapon parameters, including the active projectile (member variable) to be instantiated on the server via [Command]. The thing is, by not using [Command] on "RpcChangeWeapon" method, the instantiated bullet on server is always the same (it not changes), but on client it works fine. Otherwise, If I use [Command] on "RpcChangeWeapon", the instantiated bullet works fine on the server, but in the client the player do not change his weapon :(.

    I would love to get some help, so I can continue my final project :/ Thank you.

    public class WeaponManager : NetworkBehaviour {

    //Script References
    UIManager m_UIManager;
    BaseWeapon m_baseWeapon;
    ShootNetworkManager m_shootNetworkManager;

    //Public variables
    public GameObject m_activeWeapon;
    public GameObject m_primaryWeapon;
    public GameObject m_secondaryWeapon;

    public GameObject m_activeBullet;
    public Transform m_spawnBullet;

    //Private variables
    int m_weaponIndex = 0;
    float m_cooldown;

    //just testing
    public float F***ingfirerate = 1f;

    void Awake()
    m_shootNetworkManager = GetComponent<ShootNetworkManager> ();
    m_baseWeapon = GetComponentInChildren<BaseWeapon> ();

    void Start()
    m_UIManager = GameObject.Find ("PlayerUI").GetComponent<UIManager>();

    void Update()
    if (!isLocalPlayer)

    if (!m_activeWeapon.GetComponent<BaseWeapon> ().m_isRecharging) {
    if (Input.GetKeyDown (KeyCode.Tab)) {
    RpcChangeWeapon ();

    m_cooldown -= Time.deltaTime;

    if (Input.GetButton ("Fire1") && m_cooldown <= 0f && !m_baseWeapon.m_isRecharging)

    if (Input.GetKeyDown (KeyCode.R) && !m_baseWeapon.m_isRecharging)
    StartCoroutine(Recharge ());


    void CheckBullets()

    if (m_baseWeapon.m_loadedBullets > 0) {
    CmdFire ();
    m_cooldown = m_baseWeapon.m_fireRate;
    } else {
    StartCoroutine(Recharge ());

    void CmdFire()

    // create the bullet object from the bullet prefab
    var bullet = (GameObject)Instantiate(

    // make the bullet move away in front of the player
    bullet.GetComponent<Rigidbody>().AddRelativeForce(Vector3.forward * 400f);

    NetworkServer.Spawn (bullet);

    // make bullet disappear after 2 seconds
    Destroy(bullet, 2.0f);

    IEnumerator Recharge()
    if (m_baseWeapon.m_remainingBullets > 0) {
    m_baseWeapon.m_isRecharging = true;

    yield return new WaitForSeconds (m_baseWeapon.m_rechargebleTime);

    m_baseWeapon.m_isRecharging = false;
    int bulletsNeed = m_baseWeapon.m_maxBulletsPerCartridge - m_baseWeapon.m_loadedBullets;

    for (int i = 0; i < bulletsNeed; i++) {


    if (m_baseWeapon.m_remainingBullets <= 0) {
    m_baseWeapon.m_remainingBullets = 0;

    void SetActiveWeapon(GameObject weapon)
    m_activeWeapon = weapon;
    m_baseWeapon = m_activeWeapon.GetComponent<BaseWeapon> ();
    m_activeBullet = m_baseWeapon.m_projectile;
    m_spawnBullet = m_baseWeapon.m_projectileSpawn;

    void RpcChangeWeapon()


    switch (m_weaponIndex) {
    case 0:
    SetActiveWeapon (m_primaryWeapon);
    m_primaryWeapon.SetActive (true);
    m_secondaryWeapon.SetActive (false);
    case 1:
    SetActiveWeapon (m_secondaryWeapon);
    m_secondaryWeapon.SetActive (true);
    m_primaryWeapon.SetActive (false);


    if (m_weaponIndex >= 1)
    m_weaponIndex = -1;