- Home /
How to make gun rapid fire
Hey guys, I am trying to make the AK-47 in my game fire in automatic mode. The problem is that when I hold down the mouse button, it fires shots automatically (like it should). However, only the first of those shots does damage to any enemy. Can anyone please help me?
This is the script:
var DamageAmount : int = 5;
var TargetDistance : float;
var AllowedRange : float = 15;
function Update () {
if (AmmoDisplay.LoadedAmmo >= 1) {
if(Input.GetButton("Fire1")) {
var Shot : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), Shot)) {
TargetDistance = Shot.distance;
if (TargetDistance < AllowedRange) {
Shot.transform.SendMessage("DeductPoints", DamageAmount);
}
}
}
}
}
The deduct points only appears there in any of the scripts attached to the A$$anonymous$$-47.
I also have another script where the weapon has recoil, etc. I have tried putting the raycasting into the second script but it didn't work.
Second script...
var A$$anonymous$$ : GameObject; var AkSound : AudioSource; var $$anonymous$$uzzleFlash : GameObject; var AmmoCount : int; var Firing : int;
var UpCurs : GameObject; var DownCurs : GameObject; var LeftCurs : GameObject; var RightCurs : GameObject;
function Update () { AmmoCount = AmmoDisplay.LoadedAmmo;
if (Input.GetButton("Fire1")) {
if (AmmoCount >= 1) {
if (Firing == 0) {
A$$anonymous$$Fire();
}
}
}
}
function A$$anonymous$$Fire() { Firing = 1; UpCurs.GetComponent("Animator").enabled=true; DownCurs.GetComponent("Animator").enabled=true; LeftCurs.GetComponent("Animator").enabled=true; RightCurs.GetComponent("Animator").enabled=true; AmmoDisplay.LoadedAmmo -= 1; AkSound.Play(); $$anonymous$$uzzleFlash.SetActive(true); A$$anonymous$$.GetComponent("Animator").enabled = true; yield WaitForSeconds(0.1); var Shot : RaycastHit; $$anonymous$$uzzleFlash.SetActive(false); A$$anonymous$$.GetComponent("Animator").enabled = false; UpCurs.GetComponent("Animator").enabled=false; DownCurs.GetComponent("Animator").enabled=false; LeftCurs.GetComponent("Animator").enabled=false; RightCurs.GetComponent("Animator").enabled=false; Firing = 0; }
Answer by LASTSPARTAN101 · Nov 02, 2017 at 11:10 AM
No need to answer this question, I just solved it myself.
Answer by CletusG · Nov 27, 2019 at 09:44 PM
@LASTSPARTAN101 how did you do it? I have the same issue as you did
Answer by Marco-944 · Nov 28, 2019 at 11:19 AM
As nobody answered yet, I will try to explain my system:
I have an IEnumerator Coroutine that counts down (your interval between shots) and then sets a bool (canShoot for example) to false and spawns the bullet and then sets canShoot to true. In Update(), I have it so when I press the mouse button, it checks for canShoot and if it is true, it starts the Coroutine, which deactivates canShoot for the interval time, so there are only shots in the time i want them. After it has shot the bullet, It resets canShoot to true, so i can fire the next one. This works with automatic rifles and single shot guns Hope it was understandable
EDIT I just read the question again and realised that I made the answer to something completely different, sorry :(
For this particular problem you might be able to use it in some way too