Cook
Add sound effects to a Player/Unit
- add a variable of type
AudioClip
in a script attached to the GameObject to emit sounds (like PlayerController
)
- attach the asset to the script (ie. pass it in as parameter in the Inspector)
- add an
Audio Source
component to the Player/Unit
- in the script file, get ahold of the component and run
audio.PlayOneShot
during the event.
private AudioSource playerAudio;
public AudioClip jumpSound;
public AudioClip crashSound;
void Start() {
playerAudio = GetComponent<AudioSource>();
}
void Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
playerAudio.PlayOneShot(jumpSound, 1.0f);
}
}