Hi, I’m following a tutorial online for a shooter game. Bullets are delayed using a float variable, but only if you’re holding down the “left-click”. If you repeatedly click the left-click button as fast as you can, the bullets will leave the gun just as fast–bypassing the delay.
Is there a way to shoot ONLY if the delay counter is finished?
Here’s the code! Hopefully we don’t need to scrap the whole code for this. I do like the simplicity. Thanks in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunController : MonoBehaviour
{
public bool isFiring;
public BulletController bullet;
public float bulletSpeed;
public float timeBetweenShots;
private float shotCounter;
public Transform firePoint;
void Start()
{
}
void Update()
{
if (isFiring)
{
shotCounter -= Time.deltaTime;
if (shotCounter <= 0)
{
shotCounter = timeBetweenShots;
BulletController newBullet = Instantiate(bullet, firePoint.position, firePoint.rotation) as BulletController;
newBullet.speed = bulletSpeed;
}
}
else
{
shotCounter = 0;
}
}
}