- Home /
How an Enemy could activate a Sound?
The thing is that I have made a script that activates a sound when the player gets on its range (it's a trigger). I would like to know if there is a way to make this script works on a different way, I would like that when my enemy gets on the range of object that has this script, the sound gets activated, but at the same time, the player is able to hear it clearly no matters how far away he is from the object and the enemy. This is the script, it's very simple:
var Sound : AudioClip;
function OnTriggerEnter(){
audio.PlayOneShot(Sound);
}
It's possible?, any ideas?, sorry for the bad english:)
are you able to get any sound to play at all? there can only be 1 Audio Listener in your scene, so check that you don't have 2. also, you can only play $$anonymous$$PEG, Ogg Vorbis, WAV, AIF, $$anonymous$$OD, IT, S3$$anonymous$$, X$$anonymous$$ and as Philipp Jutzi said, it must be a 2D sound...one last thing, have you assigned your audio clip in the inspector? i see you've made it a variable but you still need to manually assign it in the inspector.
@fernandovt: There's an edit button below your question. Don't post answers when you don't answer the question.
Answer by Piflik · Aug 29, 2012 at 10:58 AM
Check if the object hitting the trigger is an enemy, so it doesn't trigger when anything else walks in there. And make the sound a 2D sound. These have the same volume, no matter how far away.
function OnTriggerEnter(item : Collider) {
if(item.tag == "Enemy")
//do stuff
}
Well, I tried but apparently I'm missing something, this is the script I put to the object:
var Sound : AudioClip;
function OnTriggerEnter(item : Collider) {
if(item.tag == "Enemy")
audio.PlayOneShot(Sound);
}
The I putted an audio source to that object (the object has a collider, with "Is Trigger" activated), and I also desactivated the option play on awake, and I dragged the sound to the script and to the audio source... The next thing I did was tagging my enemy with the tag Enemy, and I putted a collider to it, with the option "Is Trigger" activated... What I'm missing?, why the sound its not being activated??, Please Help:)
Add some debug lines to your script to see, where the problem is.
var Sound : AudioClip;
function OnTriggerEnter(item : Collider) {
print("hit by " + item.tag);
if(item.tag == "Enemy") {
audio.PlayOneShot(Sound);
print("sound is playing");
}
}
Sorry for making a new answer, I didn't notice:/, The debug lines on the script didn't appear on the screen when I played the game, so I guess it's really wrong... $$anonymous$$aybe the problem it's that the trigger options are activated in both objects?,(enemy and cube) I thank you for your patience, I know it's a tricky question, do you have any ideas?, because if the script is correct, then it's probably a problem with the enemy settings or the cube settings....
Turn off Is Trigger in the enemy - let only the cube be a trigger. Does the enemy have a Rigidbody or CharacterController component? The trigger can detect only these two components.
That none of the two lines appear means, that the collision is not detected. This can have different reasons:
1: The enemies trigger missed the sound-object's trigger completely. $$anonymous$$ake sure the enemy walks where the trigger is.
2: You have two mesh colliders, none of which are set to be convex. $$anonymous$$ake sure that at least one collider is convex. Two concave colliders don't hit each other.
3: You are missing RigidBody components. At least one object in a collision needs to be a RigidBody.
Answer by CypherGames · Aug 18, 2013 at 01:30 PM
You could use a raycast:
var Sound : AudioClip;
function Update {
var hit : RaycastHit;
var ray : Ray = new Ray(transform.position, Vector3.down);
if (Physics.Raycast(ray, hit, 2)) {
if (hit.transform.tag = "Enemy") {
audio.PlayOneShot(Sound);
}
}
}
Your answer
Follow this Question
Related Questions
Door Opens When Not In Trigger And Sound Help 1 Answer
Enemy trigger for player 1 Answer
animation and sound on collide 1 Answer
Help needed! 1 Answer
Explosion Jumpscare Script ? 1 Answer