Can someone explain this line to me
`nextPossibleShootTime = Time.time + secondsBetweenShots`
So it seems like the gun will be fired if CanShoot() is true, and if time.time is not less than nextPossibleShootTime. But how can CanShoot() ever be true if the nextPossibleShootTime is always Time.time added to something? Overall as of right now when the mousebutton is held down the audio is giving a rapid fire sound like an AK.
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class Gun : MonoBehaviour
{
public enum GunType {Semi, Burst, Auto};
public GunType gunType;
public float rpm;
public Transform spawn;
//System;
private float secondsBetweenShots;
private float nextPossibleShootTime;
void Start()
{
secondsBetweenShots = 60/rpm;
print (secondsBetweenShots);
}
public void Shoot()
{
if(CanShoot())
{
Ray ray = new Ray(spawn.position, spawn.forward);
RaycastHit hit;
float shotDistance = 20;
if(Physics.Raycast (ray, out hit, shotDistance))
{
shotDistance = hit.distance;
//shotDistance = Vector3.Distance (ray.origin, hit.point);
}
//Debug.DrawRay (ray.origin,ray.direction * shotDistance,Color.magenta,1);
nextPossibleShootTime = Time.time + secondsBetweenShots;
audio.Play ();
}
}
public void ShootContinous()
{
if(gunType == GunType.Auto)
{
Shoot ();
}
}
private bool CanShoot()
{
bool canShoot = true;
if(Time.time < nextPossibleShootTime)
{
canShoot = false;
}
return canShoot;
}
}
↧