- Home /
Question by
finlay_morrison · Nov 05, 2017 at 10:49 AM ·
c#unity 5scripting problemnetworkinghealth
Getting a mix of warnings in multiplayer
So im making a multiplayer fps and im getting some strange behaviour, when i shoot another play they are insta-killed, which shouldn't happen, then a few seconds later i get disconnected, i think the error is in these two scripts, any help is appreciate.
First script :
using UnityEngine; using UnityEngine.Networking;
[RequireComponent(typeof(WeaponManager))] public class PlayerShoot : NetworkBehaviour {
private const string PLAYER_TAG = "Player";
private PlayerWeapon currentWeapon;
[SerializeField]
public PlayerWeapon weapon;
[SerializeField]
private Camera cam;
[SerializeField]
private LayerMask mask;
private WeaponManager weaponManager;
private void Start()
{
if (cam == null)
{
Debug.LogError("PlayerShoot : No camera referenced");
this.enabled = false;
}
weaponManager = GetComponent<WeaponManager>();
}
private void Update()
{
currentWeapon = weaponManager.GetCurrentWeapon();
if (Input.GetButtonDown("Fire1") && currentWeapon.fireRate <= 0f)
{
Shoot();
} else
{
if (Input.GetButtonDown("Fire1"))
{
InvokeRepeating("Shoot", 0f, 1f / currentWeapon.fireRate);
} else if (Input.GetButtonUp("Fire1"))
{
CancelInvoke("Shoot");
}
}
}
[Command]
private void CmdOnShoot()
{
RpcDoShootEffect();
}
[ClientRpc]
private void RpcDoShootEffect()
{
weaponManager.GetCurrentGraphics().muzzleFlash.Play();
}
[Command]
private void CmdOnHit(Vector3 _pos, Vector3 _normal)
{
RpcDoHitEffect(_pos, _normal);
}
[ClientRpc]
private void RpcDoHitEffect(Vector3 _pos, Vector3 _normal)
{
GameObject _hitEffect = Instantiate(weaponManager.GetCurrentGraphics().hitEffectPrefab, _pos, Quaternion.LookRotation(_normal));
Destroy(_hitEffect, 2f);
}
[Client]
private void Shoot()
{
if (!isLocalPlayer)
return;
CmdOnShoot();
RaycastHit _hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, currentWeapon.weaponRange, mask))
{
if (_hit.collider.tag == PLAYER_TAG)
{
CmdPlayerShot(_hit.collider.name, currentWeapon.weaponDamage);
}
CmdOnHit(_hit.point, _hit.normal);
}
}
[Command]
private void CmdPlayerShot(string _playerID, int _damage)
{
Player _player = GameManager.GetPlayer(_playerID);
_player.RpcTakeDamage(_damage);
}
}
And the 2nd script :
using UnityEngine; using UnityEngine.Networking; using System.Collections;
[RequireComponent(typeof(PlayerSetup))] 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;
[SerializeField]
private GameObject[] disableGameobjectsOnDeath;
[SerializeField]
private GameObject deathEffect;
[SerializeField]
private GameObject spawnEffect;
private bool isFirstSetup = true;
public void SetupPlayer()
{
if (isLocalPlayer)
{
GameManager.instance.SetSceneCameraActive(false);
GetComponent<PlayerSetup>().playerUIInstance.SetActive(true);
CmdBroadcastNewPlayerSetup();
}
}
[Command]
private void CmdBroadcastNewPlayerSetup()
{
RpcSetupPlayerOnAllClients();
}
[ClientRpc]
private void RpcSetupPlayerOnAllClients()
{
if (isFirstSetup)
{
wasEnabled = new bool[disableOnDeath.Length];
for (int i = 0; i < wasEnabled.Length; i++)
{
wasEnabled[i] = disableOnDeath[i].enabled;
}
isFirstSetup = false;
}
SetupPlayer();
}
[ClientRpc]
public void RpcTakeDamage(int _amount)
{
if (isDead)
return;
currentHealth -= _amount;
if (currentHealth <= 0)
{
Die();
}
}
private void Die()
{
isDead = true;
for (int i = 0; i < disableOnDeath.Length; i++)
{
disableOnDeath[i].enabled = false;
}
for (int i = 0; i < disableGameobjectsOnDeath.Length; i++)
{
disableGameobjectsOnDeath[i].SetActive(false);
}
Collider _col = GetComponent<Collider>();
if (_col != null)
{
_col.enabled = false;
}
GameObject _explosion = (GameObject)Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(_explosion, 3f);
if (isLocalPlayer)
{
GameManager.instance.SetSceneCameraActive(true);
GetComponent<PlayerSetup>().playerUIInstance.SetActive(false);
}
StartCoroutine(Respawn());
}
private IEnumerator Respawn()
{
yield return new WaitForSeconds(GameManager.instance.matchSettings.respawnTime);
GameManager.instance.SetSceneCameraActive(false);
GetComponent<PlayerSetup>().playerUIInstance.SetActive(true);
Transform _spawnPoint = NetworkManager.singleton.GetStartPosition();
transform.position = _spawnPoint.position;
transform.rotation = _spawnPoint.rotation;
yield return new WaitForSeconds(0.1f);
SetDefualts();
}
public void SetDefualts()
{
isDead = false;
currentHealth = maxHealth;
for (int i = 0; i < disableOnDeath.Length; i++)
{
disableOnDeath[i].enabled = wasEnabled[i];
}
for (int i = 0; i < disableGameobjectsOnDeath.Length; i++)
{
disableGameobjectsOnDeath[i].SetActive(true);
}
Collider _col = GetComponent<Collider>();
if (_col != null)
{
_col.enabled = true;
}
GameObject _spawnEffect = (GameObject)Instantiate(spawnEffect, transform.position, Quaternion.identity);
Destroy(_spawnEffect, 3f);
}
}
Comment