- Home /
OnTriggerEnter not activating with NavMesh movement
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.
Answer by craigiedon · Jun 30, 2014 at 12:40 PM
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)
Answer by IsouEU · Jan 29, 2021 at 11:31 AM
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 ...
Fixed the problem myself.
Felling like a retard, rigidbody not enough to trigger, u must have both... a collider AND a rigidbody(disable gravity and make kinematic, and sure you will still having undesired physics effects, but sucking trigger will finally work)...
Alot of variables and calcs slowing the system only to compare if the sucking position of character with 6 points of 3 axis...
I want to quit unity, have anyway to don't lose 1 year of work ? Spend hours trying to understand a retard $$anonymous$$d, i could solve it with 3 ifs... like in seconds ! This realy sux !
Now i can see why sucking modern games need the batman computer to make a cube in 10 fps
Your answer
Follow this Question
Related Questions
How do I get a navmesh agent to stop at the edge of a collider? 0 Answers
NavMesh on collision dectection activate,On collision activate navMesh 0 Answers
How to make whole navmesh agent avoid carves rather then center of it? 0 Answers
Is there any way to count the times a NavMeshAgent avoids a collision? 1 Answer
Navmeshagent falls through the navmesh plane. Weirdest issue I have ever seen 2 Answers