- Home /
Line of sight audio
I'm extremely new to Unity and I don't really have too much of an idea of coding and scripting and such, but basically I'm making a game and you explore a labyrinth and there is also a creature roaming around. I have done the basics, but I want it so that a song (a jump scare sound) is played when the creature is a.) In the camera's view and b.) Close to the character, so that the audio clip plays when people properly see the creature.
Could anyone be of help?
Thank you so much for your time, Josh
So you are asking someone to write a script for you? Sometimes you get a script, but most of the time you get ignored. That is not what this list is about. If you do a Google for anything like "Unity3d play audio when enemy is visible" you will find many hits on this list...most with scripts to get you started scripting your own solution..
Answer by fafase · Mar 30, 2013 at 04:37 PM
Here are the steps to use:
Have a look at dot product (you can check here http://unitygems.com/basic-ai-character/#Sight)
You have pretty much all you need in the link above as it deals with distance and line of sight
Still you need to make sure that the monster is not behind the wall, so use a linecast that you cast from your player to the monster. Make sure you disable the layers (The link also has a linecast example)
Then if all returns true, you can play your scary music
if(within range && dot product angle less than angle){ if(!Linecast(player, monster)){ play music } }
Answer by mongoose665 · Mar 31, 2013 at 10:50 AM
Well so far I have this:
var appear:AudioClip;
function OnBecameVisible() {
enabled = true;
audio.PlayOneShot(appear);
}
function OnBecameInvisible () {
enabled = false;
audio.Stop();
}
I attach the script to the enemy and choose my sound for the Appear option and I still cannot hear anything?