Hey,
More questions for the forum…
I’ve currently got a situation where, when I click on a ‘character’ I need it to ‘speak’. That’s all. For now.
I have an audio clip(mp3) parented to the model of the character, and have a raycast set up on the main camera to work on the layer that the model is in, so that I can detect when the user is clicking on the model.
Now, I’ve seen information on AudioListener and AudioClip. I don’t understand the differences clearly, or how I should implement them.
I just need the player to be able to click on the model in game and have the audio clip trigger. Any help would be much appreciated.
I presume you already have the code for casting a ray in place. In that case just follow this pseudo code.
When setting up your scene, you need to make sure that the audio component is attached to the same game object as the collider.
var hit : RaycastHit;
if (Physics.Raycast(ray, hit))
{
// Get the audio component attached to the same collider
// that we hit with the ray.
if (ray.collider.audio)
ray.collider.audio.Play();
}
I’ve tried to implement this…
if (hit.collider.audio){
hit.collider.audio.Play();}
What does hit.collider.audio return? (hit is the name I’ve got for my Raycast var)
Am I taking your psuedocode too literally - do I have to fill in something for ‘collider’?
Sorry, some of the GUI is actually confusing me, still have to get used to it all.
One more question related to this -
I now want to be able to control this soundclip activating via a mouseclick. So if someone is looking at the collider in question, rather than the sound running by default, they should have to click on it.
Input.GetMouseButtonDown() was what I found to get mouseclicks.
I was trying a nested if statement within the if(hit.collider.audio) that checked for correct input from the GetMouseButtonDown() but it didn’t seem to want to work. Is there a better way to do this? Am I doing something horribly wrong?
edited to add:
I also found that the previous snippet of code (for just running the audio whenever the camera is looking at the correct collider) only initiates the audio when the camera stops looking at the collider.
I have to look, then look away to have the sound start. Any way to change that?
You might want to use the OnMouseDown event for that
function OnMouseDown() {
audio.Play();
}
Just attach this script to the game object that is supposed to play the sound and also has a collider.
Doh!
Should’ve realized it might all be that simple.
Curses abound.
But ahem, thanks a lot, now on to GUI objects.