When I shot a player and the player died, I exited the game instead of him every time

73 views Asked by At

I am creating a multiplayer shooting game in unity. For the multiplayer functionality I use photon. When I Join the room and hit another player and that player's health is 0, that player should die and leave the room, but instead, the player that did the shot leaves the lobby. Can anyone solve this issue?

Player.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.SceneManagement;
using System;

public class PlayerMovement : MonoBehaviour
{
    [SerializeField]
    private Animator animator;
    public Scene scene;
    public float angle = 5f;
    public float speedOfBackward = 2f;
    PhotonView view;
    [SerializeField]
    private int _health = 100;
    private int _takeDamage = 1;

    void Start()
    {
        scene = SceneManager.GetActiveScene();
        view = GetComponent<PhotonView>();
    }

    void Update()
    {
        if (scene.name == "Level1")
        {
            playerMovement();
        }
        else if (scene.name == "Lobby")
        {
            if (view.IsMine)
            {
                playerMovement();
            }
        }
    }

    void playerMovement()
    {
        // Move player
        if (Input.GetKey(KeyCode.Space))
        {
            animator.SetBool("IsIdle", false);
            animator.SetBool("IsFiring", true);
        }
        else if (Input.GetKey(KeyCode.UpArrow))
        {
            animator.SetBool("IsIdle", false);
            animator.SetBool("IsRunning", true);
        }
        else if (Input.GetKey(KeyCode.DownArrow))
        {
            animator.SetBool("IsIdle", false);
            animator.SetBool("IsRunningBackwards", true);
            transform.position += speedOfBackward * Time.deltaTime * Vector3.back;
        }
        else
        {
            animator.SetBool("IsIdle", true);
            animator.SetBool("IsFiring", false);
            animator.SetBool("IsRunning", false);
            animator.SetBool("IsRunningBackwards", false);
        }

        // Rotate Player
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Rotate(Vector3.up, -angle);
        }
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate(Vector3.up, angle);
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Bullet"))
        {
            Debug.Log("Health: " + _health);
            if (_health > 0)
            {
                _health -= _takeDamage;
            }
            if (_health == 0)
            {
                animator.SetBool("IsDead", true);
                animator.SetBool("IsIdle", false);
                animator.SetBool("IsFiring", false);
                animator.SetBool("IsRunning", false);
                animator.SetBool("IsRunningBackwards", false);
                StartCoroutine(PlayDeadAnimation());
            }
        }
    }

    IEnumerator PlayDeadAnimation()
    {
        yield return new WaitForSeconds(2f);
        transform.position = new Vector3(transform.position.x, -0.8094161f, transform.position.z);
        yield return new WaitForSeconds(1f);

        PhotonNetwork.Destroy(gameObject);

        PhotonNetwork.LeaveLobby();
        PhotonNetwork.LeaveRoom();
        PhotonNetwork.LoadLevel(0);


    }

}

1

There are 1 answers

1
Paweł Łęgowski On

So after ANY player is killed you call static methods from PhotonNetwork and leave room

PhotonNetwork.LeaveLobby();
PhotonNetwork.LeaveRoom();
PhotonNetwork.LoadLevel(0);

You should probably check if its LOCAL player killed before doing that.

if(view.IsMine)
{
    PhotonNetwork.LeaveLobby();
    PhotonNetwork.LeaveRoom();
    PhotonNetwork.LoadLevel(0);
}