Help with Overwatch/Ambush Function
I'm creating a cRPG type game with turn based combat. One of the things I want to add is the ability to set up an ambush when an enemy walks into range, sort of like Wasteland 2 and Shadowrun: Dragonfall had. I'm struggling with a couple of things though - I need a way to represent to the player the direction of their ambush (I already have a script that I think can do this). The second, and probably more difficult part, is I need a way to tell if the enemy has actually walked into the ambush. I thought I could just create a triangle type shape and attach it to my weapon, and make it a trigger. This would probably work, except when I was testing this method, the ambushing player was able to fire straight through walls. I have a method for checking for line of sight, but it works based on where the player is looking - if he is static, it doesn't work reliably. I suppose I could have the ambushing player turn toward the target as soon as they enter the trigger, and do the line of sight check there, but it would seem odd to have the character rotate toward an enemy he can't even see.
Has anyone else ever tackled this type of function, or have any idea how to do it? Thanks in advance.
I made some progress on this, but I'm still not sure if this is the best way to do it. I put the ambush triangle trigger object on my character, and put a script on it that does nothing but detect if something has walked into it. I also added a small cube onto the character and removed the renderer and collider, so all it has left is a transform. I created a script on this object called ambushhelper. What that script does is if something is in the ambush trigger, it tracks them by rotating toward the target and casting a ray to it. If that ray hits a wall that blocks line of sight, it does nothing. If the ray hits the target without also hitting something that blocks line of sight, it does a calculation on hit chance, then fires the weapon.
The biggest issue with this method is that the ambush and ambushhelper objects are children of the character, and there are going to be multiple characters with these children. For my test, I just used GameObject.Find to locate them by name, which obviously will not work if there are multiple instances of these objects. I may be able to solve this by shifting some scripts around.
Your answer
![](https://koobas.hobune.stream/wayback/20220612080036im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Line of Sight and Raycasting 0 Answers
How to evade Enemy AI? 1 Answer
Line of sight for multiple targets - AI - in a 2d game 1 Answer