- Home /
Machine Gun Problems
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.
If it was me, I'd put in a Debug.Log statement in to make sure it's actually seeing fire1 get pressed. Then I'd put a debug statement at the beginning of FireOneShot() to see if it's getting there.
Answer by flamy · Jan 29, 2012 at 06:21 AM
the reason y it doesnt turn on back is because you are disabling the GameObject and trying to enable it from the same script, which is not possible because the scripts in disabled gameObject will never be called unless some other script in some other gameObject makes enabled to true..
so have an instance of both muzzleFlash and the script above in someother script. Usually enabling and disabling should be done by some generic script that will be active through out the scene(just a suggestion).
Oh ok i see what you are saying i know the perfect way to fix it Thanks!
ok sorry but i now have a new problem Updated Question ^
I don't know if I speak for the community or not, but I think it's an absolutely terrible idea to completely change a question like this...If someone has your original problem they'll never be able to find it now. Please just post a new question and link to the old one if it's related
well first it is a pain to ask questions on this site and make them perfect grammar and code wise(because if they aren't people complain). And in addition NOW the question basically describes my previous question if that makes you happy. SO if you can please answer my question
never $$anonymous$$d i figured it out. I needed to start the coroutine and switch up one of my inequalities. Thanks for the help