PUN 2: Controlling two players at the same time.

Options
Hello all!
I have recently started exploring multiplayer in Unity3d. I am using PUN 2. I have a small game where two players can spawn and run around and shoot with a weapon. I have one issue that I cannot fix by myself.
Issue:
When the first player is being instantiated in the game everything is fine. The player can run around and shoot his weapon. When another client joins the room, he too can run around but when the 2nd player tries to shoot he controls the first player's weapon. Could someone lead me in the right direction? To my understanding, photonView should be used to differentiate between 'my player' and 'other players'. I don't know why this does not work here.

Here is the code for my shooting function for the player. I think the issues lie here somewhere.

Here is my Update function. Here I am calling my Shoot() function when the player holds down the left mouse button:
private void Update()
    {
        //If this is not my player then return.
        if (!photonView.IsMine) return;

        //Aim Controller
        HandleAim();

        //Equip a weapon on keypress.
        if (Input.GetKeyDown(KeyCode.Alpha1) && !hasEquip)
        {
            //Call Equip() as RPC
            photonView.RPC("Equip", RpcTarget.AllBuffered, 0);
            hasEquip = true;
        }

            if (currentWeapon != null)
            {
                if (Input.GetButton("Fire1") && coolDown <= Time.time)
                {
                    //Call shoot function. Maybe this shouldn't be RPC???
                    photonView.RPC("Shoot", RpcTarget.AllBuffered);

                    IsShooting = true;
                }
                else
                {
                    IsShooting = false;
                }
                currentWeapon.transform.localPosition = Vector3.Lerp(currentWeapon.transform.localPosition, Vector3.zero, Time.deltaTime * 8f);
        }
    }


Here is my shoot function. Here I handle some weapon recoil logic and instantiating bulletholes at the end of a raycast.
[PunRPC]
    void Shoot()
    {
        coolDown = Time.time + (1 / (loadout[currentIndex].fireRate / 60));

        //Recoil
        Vector3 pattern = muzzle.transform.position + muzzle.transform.forward * 1000f;
        pattern += Random.Range(-loadout[currentIndex].accuracy, loadout[currentIndex].accuracy) * muzzle.transform.up;
        pattern += Random.Range(-loadout[currentIndex].accuracy, loadout[currentIndex].accuracy) * muzzle.transform.right;
        pattern -= muzzle.transform.position;
        pattern.Normalize();


        //Raycast & bulletholes
        RaycastHit hit;
        if (Physics.Raycast(muzzle.transform.position, pattern, out hit, loadout[currentIndex].range, surface))
        {
            GameObject bullethole = Instantiate(bulletHole, hit.point + hit.normal * 0.0001f, Quaternion.FromToRotation(Vector3.up, hit.normal));
            bulletHole.transform.LookAt(hit.point + hit.normal);

            //Hit other player
            if (hit.collider.gameObject.layer == 11)
            {
                //Damage RPC Callback.
            }

        }

        if (isAiming)
        {
            CurrentRecoil1 += new Vector3(RecoilRotation_Aim.x, Random.Range(-RecoilRotation_Aim.y, RecoilRotation_Aim.y), Random.Range(-RecoilRotation_Aim.z, RecoilRotation_Aim.z));
            CurrentRecoil3 += new Vector3(Random.Range(-RecoilKickBack_Aim.x, RecoilKickBack_Aim.x), Random.Range(-RecoilKickBack_Aim.y, RecoilKickBack_Aim.y), RecoilKickBack_Aim.z);
        }
        else
        {
            CurrentRecoil1 += new Vector3(RecoilRotation.x, Random.Range(-RecoilRotation.y, RecoilRotation.y), Random.Range(-RecoilRotation.z, RecoilRotation.z));
            CurrentRecoil3 += new Vector3(Random.Range(-RecoilKickBack.x, RecoilKickBack.x), Random.Range(-RecoilKickBack.y, RecoilKickBack.y), RecoilKickBack.z);
        }
        //Sound
        muzzle.GetComponent<AudioSource>().Play();
    }

Comments

  • Here is a small video of the issue. When the player to the left shoots he controls his own gun. When the player to the right shoots he also controls the right player's gun.
    https://youtu.be/lvwaxuxfCVU
  • 30werewoof
    Options


    hello!! i haven't read much of your code, but i had this problem for a very long time and if yours is the same as mine then this will fix it. the scripts that control movement and the shooting need to be local to the player who has it. i followed this tutorial, if you copy paste the "player sync" script from the tutorial and make the movement script in the "local scripts" part of the component then this should fix it. if your cameras are switching on spawn, which could be the real issue, (like as soon as another players joins the cameras switch eachother which makes you see through one character but control another) just get the camera and put it in the "local objects" array part of the player sync script and it should fix that!


    Make a Multiplayer Game in Unity Using PUN 2 | Sharp Coder Blog