I think it could be a bit simpler. I wouldn't have so many operands at once because if one condition is not true, you won't have to check the others. A simple way to measure time, you can do:
public float fireRate;
private float lastShot;
void Update()
{
if ( Input.GetMouseButton( 0 ) )
{
Shoot();
}
}
void Shoot()
{
if ( lastShot + fireRate < Time.time )
{
// Pool or instantiate your bullet
// Set whatever properties you need
// Fire it off
lastShot = Time.time;
}
}
If you're afraid of the number constantly increasing you can do it a different way.
public float fireRate;
private float lastShot;
private float defaultFireRate;
void Awake()
{
defaultFireRate = fireRate;
}
void Update()
{
if ( fireRate <= 0 )
{
if ( Input.GetMouseButton( 0 ) )
{
Shoot();
}
}
else
{
fireRate -= Time.deltaTime;
}
}
void Shoot()
{
// Pool or instantiate your bullet
// Set whatever properties you need
// Fire it off
fireRate = defaultFireRate;
}
Coroutines are great but not the best for constantly changing values. They also generate garbage, so use them only when needed.
-
Personally, I believe the best option is to create Timer class and use that to set/check time for your game...
↧