Old issue: Machine gun stays disabled even when i press the fire1 button and doesn’t shoot. Any help is appreciated.
So now here is the new issue… I can fire the first round however i can’t fire after the first round because according to the inspector view it is not reloading. So here is my script
using UnityEngine;
using System.Collections;
public class MachineGun : MonoBehaviour {
public float range = 100.0F;
public float fireRate = 0.05F;
public float force = 10.0F;
public float damage = 5.0F;
public int bulletsPerClip = 40;
public int clips = 20;
public float reloadTime = 0.5F;
public Renderer muzzleFlash;
private int bulletsLeft = 0;
private float nextFireTime = 0.0F;
private int m_LastFrameShot = -1;
void Start () {
bulletsLeft = bulletsPerClip;
}
void LateUpdate()
{
if (muzzleFlash)
{
if (m_LastFrameShot == Time.frameCount)
{
muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.Range(0, 359), Vector3.forward);
muzzleFlash.enabled = true;
if (audio) {
if (!audio.isPlaying)
audio.Play();
audio.loop = true;
}
}
else
{
muzzleFlash.enabled = false;
enabled = false;
if (audio)
{
audio.loop = false;
}
}
}
}
void Fire()
{
if(bulletsLeft == 0)
return;
if(Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
while(nextFireTime < Time.time && bulletsLeft != 0)
{
FireOneShot();
nextFireTime += fireRate;
}
}
void FireOneShot()
{
Vector3 direction = transform.TransformDirection(Vector3.forward);
RaycastHit hit;
if(Physics.Raycast(transform.position, direction, out hit, range))
{
if(hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
bulletsLeft--;
m_LastFrameShot = Time.frameCount;
enabled = true;
if(bulletsLeft >= 0)
Reload();
}
IEnumerator Reload()
{
yield return new WaitForSeconds(reloadTime);
if(clips > 0)
{
clips --;
bulletsLeft = bulletsPerClip;
}
}
int GetBulletsLeft()
{
return bulletsLeft;
}
}
so there is no errors and now i have a BroadcastMessage(“Fire”) on another script so thats not a problem anymore… so any help is appreciated.