Is there a way to detect collision leave?

I know how to detect and trigger collision enter right? is there a way to detect collision leave? I’m just trying to replace raycast with collision. My use case is that, in the scene there will be multiple objects to interact with as the player gets close to them and coming from unity I know raycast is a costly method and know that collision can do better but if any other method is there to just detect objects nearby and do interaction and by interaction I mean popup a 2D screen over camera

Is this the event you are looking for? https://developer.playcanvas.com/en/api/pc.CollisionComponent.html#event:triggerleave

yes and I tried this event right when I realized that TriggerEnter must have a TriggerLeave, can you give a snippet of how I am supposed to initialize this? because mine ins’t working

Hi @sir_akc,

From the documentation, changing the triggerstart event to triggerleave -

var Trigger = pc.createScript('trigger');

// initialize code called once per entity
Trigger.prototype.initialize = function() {
    this.entity.collision.on('triggerleave', this.onTriggerLeave, this);
};

Trigger.prototype.onTriggerLeave = function(entity) {
    //do whatever
};

Hope this helps!

3 Likes