How to properly set up Collision/Trigger detection

oldmanwizard

New member
I've been stuck on this for so long. I've tried everything I can think of. I'm a novice so if anyone could give a thorough explanation that would be great thank you for your time :)
 
The trick is to have the Has Entered Trigger task reevaluate so it can execute for more than one frame. You can do this with conditional aborts. Here's a small example tree that can detect collisions:

1575852416203.png
 
I have the Has Entered Trigger task reevaluating but it still returns unsuccessfully
Capture.PNG
other.PNG
I have a object named Trigger with the tag Trigger with a collision sphere with the is trigger box checked and a rigidbody. I then have a object with a collision sphere and rigid body named head sensor that moves in and out of the trigger. Do I have the trigger set up wrong? or am I missing something else?
 
Update: I've forgotten to make the Sequence low priority. Since then I've done so and still have troubles. Now the Has Entered Trigger task shows an X with a circle around it.
 
Update: I've forgotten to make the Sequence low priority. Since then I've done so and still have troubles. Now the Has Entered Trigger task shows an X with a circle around it.

Not "low priority", it is "conditional abort", which is actually higher priority.
 
So I shouldn't set sequence to low priority?

Find the opsive video about conditional aborts. Calling it "low priority" shows a lack of understanding of how conditional aborts work.
Its working for you now, but you do not know why. This will help you in the future.
 
Find the opsive video about conditional aborts. Calling it "low priority" shows a lack of understanding of how conditional aborts work.
Its working for you now, but you do not know why. This will help you in the future.
My apologies I meant "lower priority" not "low priority".
Capture.PNG
I've taken the time to watch the videos beforehand hence why I'm here asking for a different explanation. I will rewatch the videos while I wait for a helpful response in the meantime. Thanks :)
 
Update: I've forgotten to make the Sequence low priority. Since then I've done so and still have troubles. Now the Has Entered Trigger task shows an X with a circle around it.
Using a Lower Priority abort type is correct. If you have the circle with the X around it then thing are working properly - it is being reevaluated and when the agent enters a trigger it'll output the log statement.
 
I've got it working :)

The problem was I was calling the behaviour tree from an object that wasn't colliding with the trigger and I had a misunderstanding about what Other Game Object was. It was my understanding that other game object (the object that entered the trigger) was an object that was going in and out of the trigger area. But after setting other game object to the object that had the trigger and moving the behaviour tree component to the object moving in and out of the trigger area I realized my mistake.

Thank you Justin for your patience and understanding.
 
I received a question about this over email. Here's a working example:
 

Attachments

  • TriggerExample.unitypackage
    4 KB · Views: 41
I'm struggling with getting this exact same thing working, but in 2D with a non-trigger collision.

So taking the provided TriggerExample.unity as a startingpoint and modifying it:

case A (collision with non-trigger)
- Changing the cube-object to a RigidBody-2D, Kinematic and with a box collider 2D
- Changing the trigger-object to a box collider 2d, IsTrigger not set, changing z to 0, x to 5
- Disabling the "left" part of the tree ( need to manually move)
- Changing the behaviour-tree to a Has Entered Collision 2D
-> moving the cube into the trigger in the x-direction (manually) does not work (HasEnteredCollision2D does not become green when they collide)
collider2d.png collider2d-b.png



case B (collision with trigger), so changing:
- the trigger-object and IsTrigger set ( so now it's actually a trigger)
- change the behaviour tree to a "Has Entered Trigger 2D"
-> moving the cube into the trigger (manually) does work! ( HasEnteredTrigger2D becomes green the moment they collide).
trigger2d.png

Any idea why this doesn't work? I mean, if it works for the combination "collision with object with IsTrigger and HasEnteredTrigger2D", I'd assume it would also work for the combination "collision with object not-IsTrigger and HasEnteredCollision2D" :unsure:


Countertest:
case C: Taking the original TriggerExample.unity as a starting point and:
- changing the trigger to IsTrigger false
- changing the behaviour-tree to a HasEnteredCollision (3D)
- running it (with automatic movement)
-> this works!
 
Okay, explaining the problem to someone (as often) helped, I stumbled over the solution just after posting :ROFLMAO:. For anyone else running into this, you need to enable "Full Kinematic Contact" to get "kinematic/kinematic + kinematic/static collisions" for the Cube... Enabling that and then letting them collide works (HasEnteredCollision2D becomes green the moment they collide).

Thanks again for the example, that helped a lot with initial understanding how it works!
 
Last edited:
Hello there!

I am baffled because I am unable to setup trigger detection with the simplest tree possible. The only tree able to detect trigger is the one Justin provided but I'd like to avoid the Idle task as it complexifies quite a bit the tree and simply hangs my whole tree despite the abort type of the parent task. All the tasks of the tree I am building are meant to be evaluated every frame.

1622557345748.png

Behavior Tree restarting when Complete.
Behavior Tree (not) restarting when Complete​
Behavior Tree restarting when Complete
Does not work ❌
Tested with any kind of abort type
Does not work ❌
Tested with any kind of abort type
Works ✔

My very simple setup is:

- One Sphere with a non kinematic Rigidbody + Collider not set as Trigger + BehaviorTree
- Below, a box with a Collider set as Trigger and marked as static

The system allows trigger detection with a simple script attached to the Sphere, but obviously, I would like to avoid having to send a message to the behavior tree from this script.

Code:
using UnityEngine;


public class NewBehaviourScript : MonoBehaviour
{
    public void OnTriggerEnter(Collider other)
    {
        Debug.LogError("Enter collider " + other.name, other);
    }
}

Important note: In the two first trees, the OnTriggerEnter function of the HasEnteredTrigger task is not called at all when putting a breakpoint, but it is for the third tree.

Note: I saw this thread where Justin does advise to have this idle task but I would like to understand why is it required. Isn't a Repeater task keeping the tree active? Why must the tree be active to receive the Unity message?

The final tree I want to set up

1622557907593.png
 
Last edited:
You do need some type of task to keep the tree active for more than one frame so that's why the example uses the idle task. In your first two images the tree never captures the event because the conditional task is not active for more than a single frame.

Conditional aborts should be used for the collision tasks.
 
Thanks for your answer Justin.

How can I keep the Has Entered Trigger task active without hanging my whole tree?

My attempts using a parallel task with a Self abort type didn't work, the left subtree (circled in red in my previous post) does not run while the Idle task is running. I've also tried swapping the left and right subtrees under the Parallel task, same result.

EDIT: I took a look a the package you sent in this thread and tried to replicate the structure of the tree in mine with the additional repeater tasks above the Sequence & Selector tasks of the left subtree. It now works, but the tree is quite complicated and I don't think the designers I work with will be able to build such tree when they will need to.

Working tree VS ideal tree:

1622638722657.png
 
Last edited:
You will need to use a conditional abort instead of a parallel task to keep it active. You could however have that conditional abort task under a parallel task and then do other things in your tree.
1622624705413.png
 
Top