Hello together,
first of all thanks for the great course!
I only have one issue when i start firing (left Mouse_Button or Space) the player doesn’t stop when i release the button.
I watched the lesson several times and can’t find the problem here is my code:
public class LD_Shooter : MonoBehaviour
{
[Header(“General”)]
[SerializeField] GameObject projectilePrefab;
[SerializeField] float projectileSpeed = 10f;
[SerializeField] float projectileLifetime = 10f;
[SerializeField] float firingRatePlayer = 1f;
//[SerializeField] float baseFiringRate = 0.2f;
[Header("AI")]
[SerializeField] float firingRateAiMin = 1f;
//[SerializeField] float minimumFiringRate = 0.1f;
[SerializeField] float firingRateAiMax = 0.4f;
[SerializeField] float firingRateAI = 0.2f;
[SerializeField] bool useAI;
public bool isFiring;
LD_AudioPlayer audioPlayer;
Coroutine firingCoroutine;
private void Awake()
{
audioPlayer = FindObjectOfType<LD_AudioPlayer>();
}
void Start()
{
if(useAI)
{
isFiring = true;
}
}
void Update()
{
Fire();
}
void Fire()
{
if(isFiring && firingCoroutine == null)
{
firingCoroutine = StartCoroutine(FireContinuously());
}
else if(!isFiring && firingCoroutine != null)
{
StopCoroutine(firingCoroutine);
firingCoroutine = null;
}
}
IEnumerator FireContinuously()
{
while(true)
{
GameObject instance = Instantiate(projectilePrefab, transform.position, Quaternion.identity);
Rigidbody2D rb = instance.GetComponent<Rigidbody2D>();
if (rb != null)
{
rb.velocity = transform.up * projectileSpeed;
}
Destroy(instance, projectileLifetime);
audioPlayer.PlayShootingClip();
//audioPlayer.GetInstance().PlayShootingClip();
//float timeToNextProjectile = Random.Range(baseFiringRate - firingRateVariance, baseFiringRate + firingRatePlayer Variance);
//timeToNextProjectile = Mathf.Clamp(timeToNextProjectile, minimumFiringRate, float.MaxValue);
//yield return new WaitForSeconds(timeToNextProjectile);
if (useAI)
{
firingRateAI = Random.Range(firingRateAiMax, firingRateAiMin);
yield return new WaitForSeconds(firingRateAI);
}
else
{
yield return new WaitForSeconds(firingRatePlayer);
}
}
}
}
Maybe someone knows a answer for my problem.
Thanks in advance
Daniel