Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Im trying to make a multiplayer game (ep 8)

im at ep 8 and im trying to make it so when i shoot the player it dies but it does not work, here is the codes in playershoot and player scrips.

hope someone knows what is wrong


using UnityEngine;

using UnityEngine.Networking;

public class PlayerShoot : NetworkBehaviour {

    private const string PLAYER_TAG = "Player";

    public PlayerWeapon weapon;

    [SerializeField]

    private Camera cam;

    [SerializeField]

    private LayerMask mask;

    void Start ()

    {

        if (cam == null)

        {

            Debug.LogError("PlayerShoot: No camera referenced!");

            this.enabled = false;

        }

    }

    void Update ()

    {

        if (Input.GetButtonDown("Fire1"))

        {

            Debug.Log("SHOOT!");

            Shoot();

        }

    }

    [Client]

    void Shoot ()

    {

        RaycastHit _hit;

        if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask) )

        {

            if (_hit.collider.tag == PLAYER_TAG)

            {

                CmdPlayerShot(_hit.collider.name, weapon.damage);

            }

        }

    }

    [Command]

    void CmdPlayerShot (string _playerID, int _damage)

    {

        Debug.Log(_playerID + " has been shot.");


        Player _player = GameManager.GetPlayer(_playerID);

        _player.RpcTakeDamage(_damage);

    }


}





using UnityEngine;

using UnityEngine.Networking;

using System.Collections;

public class Player : NetworkBehaviour

{


    [SyncVar]

    private bool _isDead = false;

    public bool isDead

    {

        get { return _isDead; }

        protected set { _isDead = value; }

    }


    [SerializeField]

    private int maxHealth = 100;


    [SyncVar]

    private int currentHealth;


    [SerializeField]

    private Behaviour[] disableOnDeath;

    private bool[] wasEnabled;


    public void Setup ()

    {

        wasEnabled = new bool[disableOnDeath.Length];

        for (int i = 0; i < wasEnabled.Length; i++)

        {

            wasEnabled[i] = disableOnDeath[i].enabled;

        }


        SetDefaults();

    }


        //void Update ()

    //{

        //if (!isLocalPlayer)

            //return;


        //if (Input.GetKeyDown(KeyCode.K))

        //{

            //RpcTakeDamage(99999);

        //}

    //}


    [ClientRpc]

    public void RpcTakeDamage (int _amount)

    {

        if (isDead)

            return;


        currentHealth -= _amount;

        Debug.Log(transform.name + " now has " + currentHealth + " health");


        if (currentHealth <= 0)

        {

            Die();

        }

    }


    private void Die()

    {

        isDead = true;


        for (int i = 0; i < disableOnDeath.Length; i++)

        {

            disableOnDeath[i].enabled = false;

        }

       

        Collider _col = GetComponent<Collider>();

        if (_col != null)

            _col.enabled = false;


        Debug.Log (transform.name + " is DEAD!");


        StartCoroutine(Respawn());

    }


    private IEnumerator Respawn ()

    {

        yield return new WaitForSeconds(GameManager.instance.matchSettings.respawnTime);


        SetDefaults();

        Transform _spawnPoint = NetworkManager.singleton.GetStartPosition();

        transform.position = _spawnPoint.position;

        transform.rotation = _spawnPoint.rotation;

        Debug.Log(transform.name + " respawned");

    }

    public void SetDefaults ()

    {

        isDead = false;

        currentHealth = maxHealth;


        for (int i = 0; i < disableOnDeath.Length; i++)

        {

            disableOnDeath[i].enabled = wasEnabled[i];

        }


        Collider _col = GetComponent<Collider>();

        if (_col != null)

            _col.enabled = true;

    }

}

Sign In or Register to comment.