OnTriggerEnter() conditional not fired until I select Character in Editor?! Fine thereafter...

VladD

New member
Hi,

I have a really weird thing happening, and I can't seem to find out the reason. I believe I have the logic set up correctly, but the result suffers from a mysterious bug. Maybe you can help me resolve this?

Set Up:
I have a Monobehavior Script Class that creates a SphereCollider and sets isTrigger=true in the OnAwake() function.
I have a BehaviorTree(external behavior tree) that has a Conditional OnTriggerEntered running on a Sequence with abort type= both.

Expected Result:
The character is spawned in the scene, and makes his own Trigger in the first frame. This registers the OnTriggerEntered() callback...so the Behavior should fire that when entered from then on.
The Sequence abort type=both should make this condition re-evaluate WHENEVER it happens, regardless of the branch it's currently executing.

Result:
This is completely ignored when I first load the scene programmatically. The OnTriggerEntered conditional is always failed, even though the character has a trigger.
When I switch to the Scene view and select the character in the editor while in PlayInEditor mode...suddenly everything works perfect!!
I can't really debug this...because when I select the character, everything works perfectly. And before that, I can't see the BehaviorTree running (because the character is not selected). Kind of a chicken-or-the-egg problem...

What I've tried:
I thought maybe it was an order-of-script-execution thing...maybe the trigger doesn't exist yet when the OnTriggerEntered conditional is registered?? And so it doesn't know about it?
I've tried to Disable/Enable the BehaviorTree to 'reset' it...thinking somehow it was reset by me 'selecting' the character in PlayInEditor mode...and I could emulate this behavior AFTER creating the Trigger.
No dice.

Questions:
What can you tell me about this?
Is there difference between triggers created and serialized in the Editor vs created at runtime?
Is there a way to explicitly register the Trigger and OnTriggerEntered conditional to know about each other at runtime?
What could be happening in PlayInEditor mode when I select the GameObject with the BehaviorTree? How can I re-create this at runtime (without the Behavior Designer View, of course...but just whatever "reset" logic is happening, because that WORKS!).
Here's the top of my BehaviorTree....BlockingAction is always false, and the IsCalm? branch is always evaluated (until I select the GO in Editor)...
TriggerEntered_BT.png
 
Your agent is likely entering the trigger before/at the same frame as the behavior tree is started, which will mean that the trigger will be missed. You'll want to make sure your behavior tree is active before you enter the trigger.
 
Thanks for the reply, Justin. For full disclosure, the character entity with this Behavior Tree is well away from any collisions OnAwake(). The behavior that's being scripted is a "bump" cycle, where if the Player collides with the NPC character...the NPC character goes into a state where they play an animation of them backing up, etc.

So, the NPCs are going about their Behaviors successfully, but should react to OnTriggerEnter() when the player gets too close(collide with their Trigger). But, the result I described above happens: basically everything is ignored until I try to debug by selecting the NPC and bringing up the Behavior Designer window. Then, automagically, everything works from then on in the same session.

I ask again: is there a way to do whatever this process is doing, specifically:
1) Hard register the OnTriggerEntered() event myself, in a call at runtime (to ensure it actually happens at the right time).
2) Re-start the BehaviorTree from the beginning, in a call at runtime?
 
I think I got this working. I guess it may not have been the trigger registering problem at all?!

I tested against a bare-bones "player" (just a GameObject with a sphere collider set to "trigger", and a rigid body, and setting the tag to the one I'm listening for). Then I dragged this into the NPC while PlayInEditor mode. Worked 100% of the time!

I think the PlayerController component created capsule collision is messing with my collisions...not letting the player get close enough to actually enter the trigger on the NPC. It pushes away the NPC, but doesn't actually contact the NPC's "bump" trigger, which was exactly the same size as the NPC-to-Player collision. Upping the radius of the NPC's trigger about 20% helped! I don't know why bringing up the BehaviorDesigner window even had anything to do with it. My only guess is that maybe it slowed down frame-rate enough in Editor, and with my Physics Settings let the objects intersect...like the slower frame-rate let the object travel deeper into the collision and finally reach my "Bumped" trigger. Its a guess, because you wouldn't think the two would ever be connected.

Sorry, this one should have been obvious. But, if anyone's got a similar problem, here's your trouble-shooting checklist:
1) Make sure the GameObject with the BehaviorTree has a Collider set to trigger=true.
2) Make sure the GameObect you want to collide with it has a Collider component itself.
3) Make sure the colliding G.O. has a RigidBody component, or is using a unity CharacterController (which has that, implicitly).
4) Make sure your radii allow collisions to actually happen!!! Check other non-trigger colliders to see they aren't keeping your GameObjects from actually intersecting their triggers. Make your "trigger" colliders 10%-20% bigger, just in case.

Thanks for the support. This is a good product.
 
Top