- Home /
FirstPerson and Audioplay???
So, I am working on a first person museum-esque project. My problem is that I would like it so that whenever there is a specific sphere, the player can mouse over it, it'll get highlighted, and then a certain audioclip will play. Any ideas? Any would be appreciated :)
Try to break down your problem into individual steps.
The player looks at something (Raycast)
Object hit by raycast is tagged as "objectThatShouldPlayAudio"
Tell that object to "highlight" itself. (This can be done many ways, either manipulating it from your controlling script, or defining a public method in a script attached to the object which you call)
Tell that object to start playing its clip. (Again, can be done many ways. You could define every known clip in your controlling script and play those, or you can have audio clips referenced in a script attached to the object. Similar to before, you can call a method attached to the object that tells it to start playing its clip)
Any ideas on how to script that? Also, is there an existing script to teleport you when you impact an empty gameobject, to go to another scene?
Answer by HYPERSAVV · Dec 19, 2014 at 02:43 AM
Bit of a disclaimer: I am at work, will not be able to compile this. Treat this as pseudo-code.
This is what you would use to tell that you've "looked" at an object.
void FixedUpdate()
{
RaycastHit2D hit = Phsyics2D.Raycast(transform.position, transform.forward);
// This assumes you have set up the tags appropriately
if(hit.collider.tag == "NoiseyObject")
{
hit.gameObject.GetComponent<NoiseyObjectScript>().Highlight();
hit.gameObject.GetComponent<NoiseyObjectScript>().PlayNoise();
}
}
Elsewhere, attached to your objects that make noise, you have a script attached that defines the following:
public void Highlight()
{
// "Highlight" the object
}
public void PlayNoise()
{
// audio is an AudioSource you've defined elsewhere
if(!audio.isPlaying())
{
audio.Play();
}
}
Keep in mind that these will be called for each Update cycle, which is why we do a check to see if audio has started already. You could add a flag within the second script to say "hey I'm being looked at right now" that your first script can query, and if so move on.
2 things I am confused about: (sorry to be needy :/ )
When you say: //highlight the object
Would I add the void FixedUpdate there?
2 . Is there a way to put this all into one script (with vars for the gameobject and audio) ?
Thanks for your help!
I'm not sure what you want to do for highlighting an object, so that's where the logic would go to do that. If you want to change the color, add a glow to it, yada yada, you would do that there.
I don't see why you couldn't put this all in one script, but I personally would not. An object that knows how to highlight itself should implement that logic, and another object should tell it to do it, not how to do it.
If you felt tempted to make it one script, you would need to pass a reference to the object into the method like so
public void HighlightObject(GameObject gob)
{
// Highlight object
}
This way you would perform the logic of highlighting an object on an object passed in as a parameter (ins$$anonymous$$d of the implied "this" if it were just attached as a separate script).
Your answer
Follow this Question
Related Questions
Unity 2d mobile android GUI texture button 1 Answer
ammo count help 1 Answer
Why is networkbehaviour underlined in green? 1 Answer
Writing in text box without selecting it? 1 Answer
Alpha cutoff for a GUI Texture? 2 Answers