- Home /
What is the best way to do stealth in Unity?
I was thinking of creating a FPS game that has a stealth element and I was wondering what the most efficient way to do it would be. At the moment I've been considering having a trigger on the player object that, when it collides with and enemy sends a message that tells the enemy to attack. A problem with this is that the trigger can go through other objects. Is there a way of having a ray cast that the player sends out and triggers the enemies? Or is it better to just have each enemy send out a ray cast that searches for the player?
What is wrong with the trigger going over other objects ? You could surely just specify your OnTriggerEnter function to only react to "enemies" within the area and ignore anything else ?
I would assume the problem with the trigger going over objects is that he doesn't want it going through walls and such. (Which would incorrectly alert enemies in adjacent rooms etc)
Answer by StephenLTGAMES · Nov 30, 2010 at 03:07 PM
I don;t know how to program it at moment. The best way to do it however would probley be.
if the raycast from object A (enemy) sees Object B(player)
Check for blocking objects in vision. (e.g cover).
if raycast is blocked player is not seen
if raycast is not blocked player is seen
do action (Raise alert lv based on timer)
if enemy saw player for less than 3 seconds before they disappered alert lv =1
do action.
if enemy saw player for less than 5 seconds before they disappered alert lv =2
do action
alternative.
you could also attach an camera to an enemy but make it not controllable and move the head of the enemy character on the enemys current path thus moving the camera.
then use if camera sees (tagged player) do action (attack player).
paths are often used in stealth games to allow you to do sneak attack and kills to enemy ai.
i hope you find this useful. for the thought proccess. I am kind of new to unity.
Answer by Meltdown · Nov 30, 2010 at 03:26 PM
You may want to rather send out a raycast from each enemy. This will simulate an enemies level of alertness.
Certain enemies may be more alert than others, and thus their raycast radius will be larger, and be able to detect the player earlier.
When your player is in stealth mode, you can simply reduce all enemies raycasts by x % of their initial alertness radius.
Answer by Rennat · Dec 02, 2010 at 06:44 AM
I would use a large sphere trigger on the player to decide which enemies are close enough to see him, then raycast for each enemy that's facing the player in that range to see if they have direct line of sight.
It may be necessary to do several raycasts for each enemy coming from different places on the player to the enemy's head. for instance one at center of mass, one at head, one on right side, one on left side, one on feet. That way if you're standing behind a crate that blocks your center of mass but your head is out in the open enemies will see you.
Answer by user-7004 (google) · Dec 02, 2010 at 05:12 AM
Thanks guys Ill look into it. I'm still learning scripting in Unity and I have only just realized how useful tags can be.