So I want to play a certain sound effect ONCE, every time a character starts talking.
How can I go about implementing this? If this can only be done with coding - can anyone help with the code (as I have no knowledge in coding.. :-( )
Thanks a lot!
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
This can be done by hooking into the OnStartSpeech custom event.
Sample script:
Thanks!
Few questions:
1.This script needs to be on a Game Object in the scene? can it be a component of the character game object prefab?
2. Do I need to substitute lineText and lineID for something else or can I just leave it as is?
3. Do I change audioClip with the name of the clip including extension (e.g. cry.mp3)?
4. Where is the audio clip file needs to be placed (i.e. in what directory)?
Thanks again for all the help...
It needs to be in the scene, but if it's placed on a prefab that's present in the scene at runtime then that'll work also.
The script itself can be left alone - at least to achieve what you posted about.
It can be anywhere, just assign it in the component's Inspector.
So I'm having some trouble setting this up correctly.
I've (see pics):
It doesn't work, and it also says the script can't be loaded (the console says the reference script is missing...)
What do I need to do to make this work?
Thanks a lot!
I think the name of the script needs to match its Class name, so you should name it "PlayAudioOnSpeech" not "GeniaSquak" (or you should rename its Class name in the script itself to "GeniaSquak")
Thanks!
It works now, but it works when ANY character talks not just genia...
Any idea how can I limit it to just genia?
Yeah, StartSpeech will get triggered every time ANY character speaks, so you need to add a check in there if you want to limit the Audio Clip to play only when a specific character speaks. You should really change the script back to Chris's original (i.e. "AC.Char character" not "AC.Char genia"), to reflect that, otherwise it will get confusing
If Genia is the Player Character then you could add a test (assuming you've renamed the variable back to "character", as I suggested) something like:
if (character == KickStarter.player)
{
AudioSource.PlayClipAtPoint (audioClip, Camera.main.transform.position);
}
If Genia ISN'T the Player Character then, assuming that their Prefab's "SpeakerLabel" is "Genia", then you could add a test something like:
if (character.speechLabel == "Genia")
{
AudioSource.PlayClipAtPoint (audioClip, Camera.main.transform.position);
}
I'm sure that there are other, and probably better, ways of achieving the same thing, but those should work
That was how I read your original request - you didn't mention this being only for a specific character.
The script name doesn't need to change, and the speaking character is provided as a parameter in the OnStartSpeech event.
Replace with the following and attach directly to the "Genia" character's root object: