Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

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

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

  1. Nosfera

    Nosfera

    Joined:
    Sep 26, 2014
    Posts:
    12
    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>();
    SetActiveWeapon(m_primaryWeapon);
    }

    void Update()
    {
    if (!isLocalPlayer)
    return;

    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)
    CheckBullets();

    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;
    m_baseWeapon.m_loadedBullets--;
    } else {
    StartCoroutine(Recharge ());
    }
    }


    [Command]
    void CmdFire()
    {

    // create the bullet object from the bullet prefab
    var bullet = (GameObject)Instantiate(
    m_activeBullet,
    m_spawnBullet.position,
    m_spawnBullet.rotation);

    // 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++) {

    m_baseWeapon.m_remainingBullets--;
    m_baseWeapon.m_loadedBullets++;

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

    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;
    }

    [ClientRpc]
    void RpcChangeWeapon()
    {

    m_weaponIndex++;

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

    m_UIManager.SetWeaponUI(m_activeWeapon.GetComponent<BaseWeapon>());

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

    }

    }
     
unityunity