I am making a stealth game where enemy movement is dictated by a NavMeshAgent and waypoints (marked by green waypoint flags). The enemy has a purple sight box as a child.
If the player walks into this sight box, the sight box’s OnTriggerEnter() function is called which draws a red line to the player and plays an “alerted” sound.
However, there are situations where the OnTriggerEnter() function doesn’t seem to activate when it clearly should. In particular, when the enemy has to significantly change direction to move from one waypoint to another, the enemy rotates to face the new point. The player can stand in the arc of this rotation and have the trigger not activate when it clearly and visibly should:
(Here the enemy is rotating from the bottom left flag in order to face and move to the bottom right flag)
I am unsure why this is occurring. I thought initially it was something to do with the code I had written within my OnTriggerEnter() function. However, after placing a print statement at the beginning of this function, I found out this wasn’t true. In the cases where the enemy fails to spot the player, the OnTriggerEnter() function does not even get called in the first place!
Further component details:
The player has a Character Controller attached with a large collision capsule surrounding it. The enemy has a NavMesh Agent, AI script, and Box Collider on the main object. The sight box child has a Box Collider and a Script with an OnTriggerEnter function in it.
Fixed the problem myself. The reason the trigger was activating sometimes but not always was that it was activating when the player moved into the sight box, but not when the sight box moved into the stationary player.
In order to have the trigger react to movement correctly, a rigid body must be placed on both the sight box and the triggering object (the player)
I am getting extremely disappointed with unity.
It was extremely difficult to make a multi input system to control the player (keyboard, click-walk, touchscreen joystick and physical joystick). The only solution I got at the moment was to activate and deactivate the “Character Controller” when moving with navmesh, and activating when navmesh finished the path or the player do another movement command.
But I’ll probably have to destroy about 10 scripts and start all over again …because:
Now working with triggers for the game logic, it works when the “Character Controller” is active, it means, the navmesh does not disable the trigger, because when the player is leaving the trigger by navmesh, the sucking “Character Controller” is disabled. I had already tried unsuccessfully a box collider as a child, and now I just try it with a rigibody … which besides drastically lowering the game’s FPS and don’t solved the problem, thanks anyway …
I’m trying to find a reason to use unity … these things should be ready to use … so that I could worry about the rest. And not trying to understand the stupid mind of whoever did it for basic things …