[SOLVED] Affect a specific object within a trigger enter

So here is the situation.

I want to turn on a triggerenter collision volume that will only effect ‘enemies’

Essentially if I press a button, it will enable the trigger volume and if an ‘enemy’ is in the trigger volume when I turn it on, they will die or whatever.

I don’t want this to affect any other object or entity. Just the specific one.

What i’ve tried is code like this but it doesn’t work. Help please :slight_smile:

KillEnemy.prototype.initialize = function() {
    this.entity.collision.on('triggerenter',this.onTriggerEnter,this);
};

KillEnemy.prototype.onTriggerEnter = function(result) {
    var enemy = this.app.root.findByName('enemy').rigidbody; 
    if(result.other.rigidbody === enemy){
        console.log('hit');
    } else {
        console.log('nothing');
    }
};
1 Like

You could add an ‘enemy’ tag to your enemy entities and then in onTriggerEnter check

result.other.tags.has('enemy') === true

2 Likes

I KNEW IT WAS SIMPLE! I’ll try this soon :smiley: thanks.

Tried it, didn’t work. :frowning:

I’ll make a test level tomorrow and see if I can make it work with simple stuff.

I’ll report back later if I figure it out.

Tried here, https://playcanvas.com/project/482038/overview/testing-grounds

Didn’t work. I even tried with a rigid body, didn’t seem to work then either. :frowning:

https://playcanvas.com/project/482038/overview/testing-grounds

https://playcanvas.com/project/525171/overview/testing-grounds-from-forums

Triggers are only triggered by non static rigidbodies by the looks of things.

Great. that helps a lot! Found a solution to the issue.

It would be nice though if I can have collision volumes effect each other without the need of rigid bodies. Maybe in an update?

Hi @Robotpencil, you mentioned you found a solution to this, can I ask what workaround you did? I wanna affect specific entity only when entering a trigger volume but it ends up affecting all objects that are entering. :frowning: thanks.

Hi @nkkll-correa and welcome! You can add a specific tag to this entity and check if the entity that entered the trigger has that tag.

if (result.other.tags.has('enemy')) {

}

Yes, I already added a tag, and checked using if(result.other.tags.has('enemy')){} or if(result.other.tags.has('enemy') === true){}; but it gives me an error “Cannot read property ‘tags’ of undefined”. Should I explain my situation here or create another post for it?

Ah okay, I think you have to remove other indeed, because this is only used with onCollisionStart and not with onTriggerEnter. Can you try the way below? Otherwise can you share a link of your project please?

if (result && result.tags.has('enemy')) {

}

Yes, it worked! It’s exactly what I needed, thanks for the help and info.

1 Like

I tried applying this to my project but it displays an error, saying Cannot read properties of undefined (reading ‘has’).

I also tried using if (result.other.tags.has(‘fail’)), it wont work unless I add rigid body.

https://playcanvas.com/project/961837/overview/chuck-quacker

Can you check it, please? it’s on level 2 scene. Thank you very much

This is as expected because you use a collisionstart event. If your don’t want to use a rigidbody you need to use a triggerenter event (without other). Please check the page below.

https://developer.playcanvas.com/en/tutorials/collision-and-triggers/

1 Like

I do also have triggerenter, making a screen pop up when entered. But I need also the player to play an animation and this.entity.play(‘chuckHurt’); does not work on my triggerVolume script.

I don’t see where you use this in your triggerVolume script(s). I only see you use this in the player script with a collisionstart event. This will only work if the other entity has a rigidbody too. You need to use triggerenter if the other entity doesn’t has a rigidbody.

I tried this before but it also displayed an error “Cannot read properties of undefined (reading ‘play’)”

TriggerVolumeF.prototype.onTriggerEnter = function(result) {

    if (result && result.tags.has('player')) {

    this.entity.sprite.play('chuckHurt');
    }

};

Because in this case this.entity is the trigger entity and not the player entity. I suggest to replace this.entity with result, because result is the player entity, which has the sprite.

1 Like

Yeah, that worked! Thanks for the help, Albertos :slight_smile:

1 Like