- Home /
Automatic weapon does not stop firing when it runs out of ammo. Using RayCast and Javascript.
I'm attempting to make an m4a1 fire via RayCast. I read through the scripting reference and understand it for the most part however I've run in to a problem. The script I am using is as follows:
var Range : float = 1000;
var Force : float = 200;
var Clips : int = 20;
var BulletsInClip : int = 60;
var ReloadTime : float = 2.2;
var BulletsLeft : int = 0;
var ShootDelay : float = 0;
var ShootCooldown : float = 0.2;
public var audioShot : AudioClip;
public var audioReload : AudioClip;
function Start()
{
BulletsLeft = BulletsInClip;
}
function Update ()
{
if(ShootDelay > 0)
{
ShootDelay -= Time.deltaTime;
}
else
{
ShootDelay = 0;
}
if(Input.GetButton("Fire1"))
{
if(ShootDelay == 0 && BulletsLeft > 0)
{
PlayShot();
BulletsLeft--;
CastRay();
ShootDelay = ShootCooldown;
}
if (BulletsLeft == 0 && Clips > 0)
{
ShootDelay = ReloadTime;
BulletsLeft = 0;
Reload();
}
}
}
function CastRay()
{
var Hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.position, DirectionRay * Range, Color.magenta);
if(Physics.Raycast(transform.position , DirectionRay , Hit, Range ))
{
if(Hit.rigidbody)
{
Hit.rigidbody.AddForceAtPosition(DirectionRay * Force , Hit.point);
}
}
}
function Reload()
{
yield WaitForSeconds(ReloadTime);
PlayReload();
BulletsLeft = BulletsInClip;
Clips--;
}
function PlayShot()
{
audio.PlayOneShot(audioShot);
}
function PlayReload()
{
audio.PlayOneShot(audioReload);
}
However when the gun runs out of ammo, it reloads but continues firing. This causes the gun to continue using up a spare clip until it has a negative amount of clips remaining, which will then stop it from reloading any more.
Any insight you guys can provide would be much appreciated.
Thanks in advance.
If you hit and release the fire button does it keep firing? Can you be more specific about the error?
Sorry, I should have specified that this occurs when holding down the fire button. If I release it, it stops firing as expected however as long as the button is held down it will continue to call the reload function until there are 0 or less clips remaining.
Answer by Chronos-L · Mar 13, 2013 at 04:08 AM
Your Reload()
function is yield
multiple time when you keep on holding "Fire1" all the time. That's why you will get negative amount of clips.
I fix it by adding a reloading
boolean to the script:
private var reloading : boolean = false;
function Update ()
{
if(ShootDelay > 0)
{
ShootDelay -= Time.deltaTime;
}
else
{
ShootDelay = 0;
}
//Add this
if(Input.GetButton("Fire1") && !reloading)
{
if(ShootDelay == 0 && BulletsLeft > 0)
{
PlayShot();
BulletsLeft--;
CastRay();
ShootDelay = ShootCooldown;
}
if (BulletsLeft == 0 && Clips > 0)
{
//Add this
reloading = true;
ShootDelay = ReloadTime;
BulletsLeft = 0;
Reload();
}
}
}
function Reload()
{
yield WaitForSeconds(ReloadTime);
PlayReload();
BulletsLeft = BulletsInClip;
Clips--;
//Add this
reloading = false;
}
Now, after this fix, I can hold on "Fire1" key. It will keep firing until ran of out ammo, reload, consume one clip, then keep going, without me releasing the key at all.
Hopes that this fix will be what you are looking for.
Answer by Moonwalker777 · Aug 18, 2013 at 09:39 AM
I've the same problem with a different script , maybe you can put an IF condition like if(animation.IsPlaying("reload"){ bullets = 0 }
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
OnGUI nullreference 0 Answers
Raycasting not working as expected 2 Answers