Skip to content

Instantly share code, notes, and snippets.

@SocketWeaver
Created June 17, 2019 00:05
Show Gist options
  • Save SocketWeaver/8c3e249ed655c4bcecdcb2caec9c2115 to your computer and use it in GitHub Desktop.
Save SocketWeaver/8c3e249ed655c4bcecdcb2caec9c2115 to your computer and use it in GitHub Desktop.
Change the Gun.cs script so that the weapon GUIs are not affected by other players weapon selection.
using SWNetwork;
using UnityEngine;
public class Gun : MonoBehaviour
{
public float TimeBetweenBullets = 0.2f;
public float Range = 100f;
public LayerMask ShootableMask;
public int Damage;
public int Ammo;
public int WeaponIndex = -1;
GUIManager guiManager;
PlayerAim playerAim;
GunEffect gunEffect;
float timer;
RaycastHit shootHit;
SyncPropertyAgent syncPropertyAgent;
NetworkID networkID;
public float effectDisplayTime = 0.2f;
void DealDamage(GameObject go)
{
Health health = go.GetComponent<Health>();
if (health != null)
{
health.GotHit(Damage);
}
}
public void AddAmmo(int count)
{
if (networkID.IsMine)
{
Ammo = Ammo + count;
guiManager.SetWeaponAmmo(WeaponIndex, Ammo);
}
}
public void AssignIndex(int index)
{
WeaponIndex = index;
AddAmmo(0);
}
private void Awake()
{
guiManager = FindObjectOfType<GUIManager>();
playerAim = GetComponentInParent<PlayerAim>();
gunEffect = GetComponent<GunEffect>();
syncPropertyAgent = GetComponentInParent<SyncPropertyAgent>();
networkID = GetComponentInParent<NetworkID>();
}
private void OnEnable()
{
if(WeaponIndex >= 0 && networkID.IsMine)
{
guiManager.SetWeaponAcive(WeaponIndex, true);
}
}
void OnDisable()
{
if (WeaponIndex >= 0 && networkID.IsMine)
{
guiManager.SetWeaponAcive(WeaponIndex, false);
}
}
void Update()
{
timer += Time.deltaTime;
if (timer >= effectDisplayTime)
{
gunEffect.StopEffects();
}
}
public void Fire()
{
if (timer >= TimeBetweenBullets && Ammo > 0)
{
DoFire();
}
}
public void DoFire()
{
AddAmmo(-1);
timer = 0f;
gunEffect.PlayEffects(playerAim.aimPoint);
if(playerAim.target != null)
{
DealDamage(playerAim.target);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment