I need my gun to have a reload delay when ever I reload.
I have a functioning reload but not with delay.
How can I do this?
Also the how do make the gun not be able to reload if my bullet count is full?
private float reloadTimer = 3;
void Update () {
if(Input.GetMouseButtonDown(0)&& Bullets>0){
Shoot(); //Start Shooting
//GunEffect.active = true;
}else{
if (Input.GetKeyDown("r")&& AmmoLeft>0){
reloadTimer = Time.time;
Bullets = Bullets+8;
AmmoLeft = AmmoLeft-1;
AmmoClips.guiText.text = AmmoLeft.ToString();
Ammo.guiText.text = Bullets.ToString();
}
}
You can use a variable to set the next shot time: add to it the shot interval when shooting, and add the reload interval after reloading:
public float shotInterval = 0.1f; // interval between shots
public float reloadTime = 0.5f; // reload time
private float shotTime; // time control
void Update(){
// only shoot when there are bullets and it's time to shoot
if (Bullets>0 && Time.time>shotTime && Input.GetMouseButtonDown(0)){
Shoot();
shotTime = Time.time+shotInterval; // set next shot time
}
// only reload when out of bullets and there are ammo clips:
if (Bullets==0 && AmmoLeft>0 && Input.GetKeyDown("r")){
Bullets += 8; // load the bullets
AmmoLeft -= 1; // decrement clip count
shotTime = Time.time + reloadTime; // set reload time
}
}