I have a question about triggerenter

hello
I’m trying to handle a triggerenter event that starts with a rigidbody attached at the beginning and then removes the rigidbody with a script after a few seconds, but it doesn’t work.
Is this not supposed to be done?

If I don’t attach the rigidbody in the editor, the trigger event works fine.

The reason I want to do this is so that the item is picked up after falling out of the air at the beginning of the game.

Looking for help!

var ItemBox = pc.createScript('itemBox');

ItemBox.prototype.initialize = function () {

    this.entity.collision.on("triggerenter", this.getItem, this);

    this.isRemove = false;
    this.removeTime = 2;
};

ItemBox.prototype.update = function (dt) {

    if(!this.isRemove){
        this.removeTime -= dt;
        if(this.removeTime <= 0){
            this.entity.removeComponent("rigidbody");
            this.removeTime = 2;
            this.isRemove = true;
        }
    }
};


ItemBox.prototype.getItem = function (result) {
    console.log(result);

    if (result ) {
        this.entity.destroy(); 
    }
};

Hi @Sibuya_Ryu and welcome!

That’s probably because a trigger entity cannot have a rigidbody component. Instead of triggerenter you can use collisionstart.

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

1 Like