Hello,
Ended up refactoring as soon as i saw they were both so similar, came up with something slightly different.
Any reason from a “best practice” we wouldn’t want to have the audio clips/volumes in different scripts like this?
public void PlayOneShotClip(AudioClip audioclip)
{
AudioSource.PlayClipAtPoint(audioclip, Camera.main.transform.position, shootingVolume);
}
then in shooter/health component
[Space]
[Header("Audio Settings")]
[SerializeField] AudioClip shootingSFX;
[SerializeField] [Range(0f, 1f)] float shootVolume = 1f;
IEnumerator FireContinuously()
{
while(true)
{
GameObject projectile = Instantiate(projectilePrefab, transform.position, Quaternion.identity);
Rigidbody2D rb = projectile.GetComponent<Rigidbody2D>();
if(rb != null)
{
rb.velocity = transform.up * projectileSpeed;
}
Destroy(projectile, projectileLifetime);
//audioPlayer.PlayShootingClip();
audioPlayer.PlayOneShotClip(shootingSFX, shootVolume);
yield return new WaitForSeconds(GetRandomSpawnTime());
}
}
}
[Space]
[Header("Audio Settings")]
[SerializeField] AudioClip hitSFX;
[SerializeField] [Range(0f, 1f)] float hitVolume = 1f;
private void TakeDamage(int damage)
{
audioPlayer.PlayOneShotClip(hitSFX, hitVolume);
health -= damage;
if (health <= 0)
{
Destroy(gameObject);
}
}