- Home /
Other
Machine gun help
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);
}
I don't spot anything that would account for your behavior in this code, but something is strange here. You say bullets 'goes to $$anonymous$$us', but nothing in this code ever decreases the number of bullets. Which means we are missing some critical code to take a look at or you've misunderstood what is going on in your code.
Did you check the initialization of FireRate in the inspector, not just in the code (assu$$anonymous$$g it is not private)?
You want to remove your second question and ask it separately.
After reading the script now I see that one line is wrong. Shoot(); I changed to InvokeRepeating thats why I get $$anonymous$$us and these problems.
I have been trying to add FireRate in almost every way I know but nothing really helps. Do you know how I can add it?
Answer by Seizure · Aug 06, 2013 at 04:00 PM
I'm not seeing where you decrement Bullets. Are you sure Bullets ever == 0? You may want to check any other scripts you have in the scene, try turning them off and see if you still have the same effect.
There is no other script that is interfering with this one. I have a problem with just the gun script. I changed it to be InvokeRepeate and that made the gun shoot really nicely but like I explained in the description that my bullets go $$anonymous$$us and the firing never stops.
I think he's right would you want to do: (Bullets>0 || Time.time> nextFire ?
I think this is from last summer and probably fixed but since new users can't bother taking care of their question, this is now back up...
Follow this Question
Related Questions
Blood on weapon. 1 Answer
How to make my enemy bleed 1 Answer
How do I use particles to make blood spurt from an enemy when you shoot it? 1 Answer
Enemy bleeding 1 Answer
On Collide Destroy Game Object 2 Answers