- Home /
Make player unable to shoot when reloading
I am trying to make it so that when you are reloading you can't shoot. I could set current ammo to 0 before currentAmmo is set to maxAmmo but I want to play a different animation when there is still a bullet left in the chamber. Any help?
var bullet : GameObject;
var weapon : GameObject;
var currentAmmo = 30;
var maxAmmo = 30;
var canFire : boolean = false;
var fireSound : GameObject;
var reloadSound : GameObject;
var tacReloadSound : GameObject;
var reloadTime : float = 1.4;
function Update ()
{
if (currentAmmo > 0)
canFire = true;
if (currentAmmo <= 0)
canFire = false;
if (currentAmmo > maxAmmo)
currentAmmo = maxAmmo;
if (Input.GetButtonDown("Fire") && canFire == true)
IsFiring ();
if (Input.GetButtonDown("Reload") && currentAmmo < maxAmmo)
Reload ();
canFire = false;
}
function IsFiring ()
{
if (currentAmmo > 0 && canFire == true)
currentAmmo --;
Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
weapon.animation.Play("Fire");
Instantiate(fireSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
if (currentAmmo == 0)
{
canFire = false;
Reload ();
}
}
function Reload ()
{
if (currentAmmo == 0)
animation.Play("TacReload");
Instantiate(reloadSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
canFire = false;
if (currentAmmo > 0)
animation.Play("Reload");
Instantiate(tacReloadSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
canFire = false;
yield WaitForSeconds(reloadTime);
canFire = false;
currentAmmo = maxAmmo;
}
Answer by jordanblythe104 · Jan 23, 2013 at 11:50 AM
Fixed it! I set the current ammo within each if statement to equal 0 until the reloadTime had counted down. Then current ammo was set to equal maxAmmo
Answer by cdrandin · Jan 23, 2013 at 08:48 AM
In the if statement you could just add "&& !isReloading" where isReloading is true when reload is called and set back to false when player does shoot.
I've tried that. What I want is for the player not to be able to shoot while the reload animations are playing. I just can't seam to figure it out
Answer by farspacer · Jul 28, 2014 at 02:59 PM
That's how I did this:
public float ammo; //chars ammo
public float clip; //chars clips
public bool reload = false; //by default we're not reloading
void Update ()
{
//shoot if there is ammo
if (ammo > 0)
{
Shoot ();
}
//reload if there is no ammo or "R" button is pressed
if (ammo == 0 | Input.GetKeyDown(KeyCode.R))
{
StartCoroutine (Reload ());
}
}
void Shoot () //inable shooting
{
if (Input.GetKey(KeyCode.Mouse0) && Time.time > nextFire && reload == false)
//shooting code here
}
IEnumerator Reload () //reload the weapon
{
reload = true;
yield return new WaitForSeconds(2);
if (clip > 0)
{
ammo = 35;
clip--;
}
reload = false;
}
Now when my character's reloading he can't shoot.