Game Development Community

triggering a sound when enemy gets close to player

by Craig Lallathin · in Torque Game Engine Advanced · 02/18/2010 (10:27 pm) · 5 replies

I am trying to make a sound play when the ai player comes within about 10 feet of the player and continue playing until the ai player is dead.

Is there an easy way to do this?

If so please help.

#1
02/18/2010 (11:08 pm)

If you don't have many (active) AI players, you could just do a sphere/sphere or sphere/box intersection between the AI players and the human player to detect when he is in range, then play the sound. Doesn't take occlusion into account, of course, so the player may in fact be on the other side of a wall and be invisible to the player.
#2
02/19/2010 (12:11 am)
that sounds sweet but I'm not sure how to go about doing that.

#3
02/19/2010 (4:02 pm)
After all player (AI and human) moves, take a sphere centered on the human player and with a range corresponding to the sound activation range. Then go through all (active) AI players. For each AI player, intersect either its bounding box or bounding sphere (faster) with the sound activation sphere. If you have an intersection, break, and play the sound, if it is not already playing.

If you wind up with no intersection, stop the sound.
#4
02/19/2010 (4:30 pm)
Quote:For each AI player, intersect either its bounding box or bounding sphere (faster) with the sound activation sphere.
Range check instead? Wouldn't take bounding boxes into account, but you could fudge it by a factor equivalent to the bounding box's average dimension.

This could be done in script with a scheduled function to loop through a list of AI characters who activate sound. Code-side would obviously be better, and would allow you to use sphere collision (not, as far as I know, available through script. Oh, maybe through container radius search - but then you're not dealing with a list of objects any more, but a container).
#5
02/19/2010 (4:38 pm)

Indeed, taking simple distance would be fastest and thinking about it, taking bounding spheres would probably be just a waste.