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
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.