Skip to content

Instantly share code, notes, and snippets.

@DeKoServidoni
Created August 29, 2021 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DeKoServidoni/c43fe0e6296362e8ca840aee0eb52739 to your computer and use it in GitHub Desktop.
Save DeKoServidoni/c43fe0e6296362e8ca840aee0eb52739 to your computer and use it in GitHub Desktop.
Player Sound Effects
using UnityEngine;
public class PlayerSFX : MonoBehaviour {
[SerializeField] AudioSource _audioSource;
[Header("Sounds")]
[SerializeField] SoundSO _jumpSFX = null;
[SerializeField] SoundSO _attackSFX = null;
[SerializeField] SoundSO _hitSFX = null;
public void PlayJumpSound() {
PlayOneShot(_jumpSFX.clip, _jumpSFX.volume);
}
public void PlayAttackSound() {
PlayOneShot(_attackSFX.clip, _attackSFX.volume);
}
public void PlayHitSound() {
PlayOneShot(_hitSFX.clip, _hitSFX.volume);
}
private void PlayOneShot(AudioClip clip, float volume) {
if (_audioSource == null)
return;
_audioSource.PlayOneShot(clip, volume);
}
}
using UnityEngine;
[CreateAssetMenu(menuName = "Sounds/SFX")]
public class SoundSO: ScriptableObject {
[SerializeField] string _name;
[SerializeField] AudioClip _clip;
[SerializeField][Range(0f, 1f)] float _volume = 0.0f;
[SerializeField][Range(.1f, 3f)] float _pitch = 0.1f;
[SerializeField] bool _loop = false;
public new string name { get { return _name; } }
public AudioClip clip { get { return _clip; } }
public float volume { get { return _volume; } }
public float pitch { get { return _pitch; } }
public bool loop { get { return _loop; } }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment