- Home /
Trouble With Disableing a Function
Okay so I have been working on an FPS, (like most people) and i need to disable my Reload function while I'm shooting my gun. I can get it to work by putting if(!Firing){return} at the beginning of the function and it won't reload at all. Even when I'm all out of ammo. Any help would be appreciated.
var fireRate : float = .5;
var bulletShell : Transform;
var BulletShellSpead : float;
var MuzzleFlash : Transform;
var Smoke : Transform;
var Sound : AudioClip;
var strayFactor : float;
var bulletPrefab : Transform;
var bulletSpeed : int;
var GrenadeSpeed : float;
var Grenade : Transform;
var GrenadeAmmo : int;
var MaxAmmo : int;
var CurrentClip : int = 10;
var CurrentExtraAmmo : int = 100;
var ClipSize : int = 10;
var ReloadTime : float;
var ClickSound : AudioClip;
var ReloadSound : AudioClip;
var Reloading : boolean = false;
var Firing : boolean = false;
private var nextFire = .5;
function LateUpdate()
{
if(GrenadeAmmo < 0)
GrenadeAmmo = 0;
if(CurrentClip < 0)
CurrentClip = 0;
if(CurrentClip > MaxAmmo)
CurrentClip = MaxAmmo;
if(Input.GetButtonDown("Fire1")&& CurrentClip <= 0)
{
NoAmmo();
}
}
function Update()
{
if(Input.GetKeyDown(KeyCode.R))
{
Reload();
}
if(Input.GetKeyDown(KeyCode.E))
{
GrenadeThrow();
}
if(CurrentClip > 0)
{
if(Input.GetButtonDown("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Firing = true;
print("ouch");
Shoot();
var bulletShell = Instantiate(bulletShell, GameObject.Find("Spawn3").transform.position, Quaternion.identity);
bulletShell.rigidbody.AddForce(transform.right * BulletShellSpead);
var MuzzleFlash = Instantiate(MuzzleFlash, GameObject.Find("Spawn").transform.position, Quaternion.identity);
var Smoke = Instantiate(Smoke, GameObject.Find("Spawn").transform.position, Quaternion.identity);
animation.Play("PistolShoot");
audio.PlayOneShot(Sound);
Firing = false;
}
}
}
function GrenadeThrow()
{
if(GrenadeAmmo > 0)
{
var grenade = Instantiate(Grenade, transform.position, Quaternion.identity);
grenade.rigidbody.AddForce(transform.forward * GrenadeSpeed);
}
GrenadeAmmo --;
}
function Shoot()
{
var randomNumberX = Random.Range(-strayFactor, strayFactor);
var randomNumberY = Random.Range(-strayFactor, strayFactor);
var randomNumberZ = Random.Range(-strayFactor, strayFactor);
var bullet = Instantiate(bulletPrefab, GameObject.Find("Spawn").transform.position, transform.rotation);
bullet.transform.Rotate(randomNumberX, randomNumberY, randomNumberZ);
bullet.rigidbody.AddForce(bullet.transform.forward * bulletSpeed);
bullet.tag = "Bullet";
CurrentClip --;
}
function Reload()
{
if(!Firing)
{
return;
}
if(CurrentExtraAmmo <= 0)
{
NoAmmo();
}
else
{
if(CurrentClip <= 14)
{
Reloading = true;
animation.Play("Reload");
audio.PlayOneShot(ReloadSound);
}
yield WaitForSeconds(ReloadTime);
Reloading = false;
if(CurrentExtraAmmo >= ClipSize - CurrentClip)
{
CurrentExtraAmmo -= ClipSize - CurrentClip;
CurrentClip = ClipSize;
}
if(CurrentExtraAmmo <= ClipSize - CurrentClip)
{
CurrentClip += CurrentExtraAmmo;
CurrentExtraAmmo = 0;
}
}
}
function NoAmmo()
{
Firing = false;
animation.Play("Emepty");
audio.PlayOneShot(ClickSound);
}
function OnGUI()
{
if(CurrentClip <= 5)
{
GUI.contentColor = Color.red;
}
GUI.Box(Rect(Screen.width / 1.16, Screen.height / 80, 100, 20), ""+ CurrentClip + "/" + CurrentExtraAmmo);
}
Answer by Berenger · Jun 30, 2012 at 04:41 AM
Firing = false; should be when Input.GetButtonUp("Fire1) is true, not Down.
By the way, not that it's important but, why do you use Update and LateUpdate ? I don't see a real reason here, besides being called after Update, but putting it at the end of Update would have the same effect.
I didn't mean to put my question in the answers. If you see just ignore it. So would I add a GetButtonUp? Because that's what it sounds like.
And the answer to your question, it's easier to find in LateUpdate.
I had something like that in $$anonymous$$d :
if( GetButtonDown ){
isFiring = true;
// rest of the code
}
if( GetButtonUp )
isFiring = false;
Okay that makes sense, and I had done something like that, but removed it because I just needed to put the false statement to work better at the end of the firing sequence. And it didn't glitch and say that I was firing even though I wasn't. I tried to put that kind of thing in there, but it still won't allow me to reload.
Shouldnt you return from Reload() WHEN you are firing? Why do you exit when you are NOT firing?
Okay yeah but I still can reload while I'm firing! And now that I removed this !, I can reload now, but I can still reload while I'm firing.
Your answer
Follow this Question
Related Questions
Function reload doesnt work 1 Answer
Reload/Shoot Animation 3 Answers
Java to C# conversion problem 1 Answer
BCE0005: Unknown identifier: 'Fire'. / Secondary shot? 1 Answer
C# - Ammo in Shoot Array?! 1 Answer