Okay so recently i asked about my gun that’s bugging out and i got one awesner and it said:
Just glancing over it, it looks like:
yield WaitForSeconds(FireRate);
inside your fire function could be giving you some trouble! It doesnt count down ammo before after the fire rate. But I would try putting the delay somewhere else, so that you can’t shoot another bullet until the firerate is done.
Here is my code:
var Bullet : Rigidbody;
var Spawn : Transform;
var BulletSpeed : float = 1000;
var ReloadTime : float = 2;
var AmmoInMag : float = 30;
var IsFullAuto = true;
static var AmmoLeft : float;
static var ReloadTTime : float;
private var CanFire = true;
var FireRate = 0.1;
static var IsReloading = false;
function Start () {
AmmoLeft = AmmoInMag;
ReloadTTime = ReloadTime;
}
function Update () {
if(IsFullAuto == false) {
if(Input.GetButtonDown("Fire1")){
if(AmmoLeft > 0) {
BroadcastMessage("FireAnim");
Fire();
}
}
}
else{
if(Input.GetButton("Fire1")){
if(AmmoLeft > 0) {
BroadcastMessage("FireAnim");
Fire();
}
}
}
if(AmmoLeft == 0)
{
Reload();
}
if(AmmoLeft < 0){
AmmoLeft = 0;
}
}
function Fire () {
if(CanFire == true && IsReloading == false){
var bullet1 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
bullet1.AddForce(transform.forward*BulletSpeed);
CanFire = false;
yield WaitForSeconds(FireRate);
CanFire = true;
AmmoLeft -= 1;
audio.Play();
}
}
function Reload () {
CanFire = false;
IsReloading = true;
BroadcastMessage("ReloadAnim");
yield WaitForSeconds(ReloadTime);
IsReloading = false;
CanFire = true;
AmmoLeft = AmmoInMag;
}
How do I add a delay?
Thank you.
PS here is a link to the problem post:
PPS here is a link to a YouTube video showing the problem: