I have a script for a machine gun but every time I press the fire button the gun keeps shooting and never stops even if there is no more bullets it just goes to minus.
I have managed to change it to stop when I release the button but the gun shoots out all the ammo within a second.
The machine gun should be more to shooting and stoping when button is help and released and shoot per half a second.
Please help me.
This is my script.
public GameObject BulletMark;
public float shotInterval = 0.1f; // interval between shots
public float reloadTime = 0.5f; // reload time
public float FireRate;
public float GunRecoil = 10;
private float shotTime; // time control
void Update () {
if (Bullets>0 && Time.time> nextFire && Input.GetMouseButtonDown(0)){
nextFire = Time.time + FireRate;
InvokeRepeating("Shoot", 0.1f, 0.1f); //Start Shooting
//GunEffect.active = true;
}else{
if (Bullets==0 && AmmoLeft>0 && Input.GetKeyDown("r")){
Bullets = Bullets+8;
AmmoLeft = AmmoLeft-1;
AmmoClips.guiText.text = AmmoLeft.ToString();
Ammo.guiText.text = Bullets.ToString();
shotTime = Time.time + reloadTime; // set reload time
}
}
}
void Shoot(){
animation.Play();
audio.Play();
//Normal Raycast //Add a random value to mouse position(r
ray = Camera.main.ScreenPointToRay(Input.mousePosition+Random.insideUnitSphere*GunRecoil);
if(Physics.Raycast(ray, out hit, 10)){
//Declear new GameObject variable
GameObject BulletHole;
//Create the shootmark prefab at the hit point
BulletHole = Instantiate (BulletMark, hit.point+(hit.normal*0.001f), transform.rotation) as GameObject;
//Rotate the created object at the face of the normal LookAt (hit.point+hit.normal)
BulletHole.transform.LookAt(hit.point+hit.normal);
BulletHole.transform.parent=hit.transform;
hit.transform.gameObject.SendMessage("Hit", 50, SendMessageOptions.DontRequireReceiver);
}