Unable to reuse collision script even after removing rigidbody component

Here is simple project where Box fall on Ball.

Box have dynamic rigidbody.
Ball have collider with triggerenter event.

When Box touch to Ball. I am adding dynamic rigidbody to Ball.

But after clicking on reset button, I am repositioning the entities at original place also i am trying to remove dynamic rigidbody from Ball to enable the collision component.

But it does not work…

So i created simulation []: https://playcanvas.com/editor/scene/943957

It is necessary for my project to reuse collider component.
Am i doing anything wrong?
Kindly help…

Thanks

Any one can help?

Not sure why this is happening, but to fix your issue you can remove the collision component as well, add it back in place in code and also add again the trigger listener:

        this.ballEntity.removeComponent("rigidbody");
        this.ballEntity.removeComponent("collision");
        
        this.ballEntity.addComponent("collision", {
            type: "sphere",
            radius: 0.5
        });
        
        this.ballEntity.collision.on('triggerenter', function(){
            console.log('After reset, this works');
        }, this);
1 Like

Bug Maybe?

Btw Thanks @Leonidas You saved my life! :relieved:

this.ballEntity.removeComponent("rigidbody");
this.ballEntity.removeComponent("collision");

this.ballEntity.addComponent("collision", {
    type: "sphere",
    radius: 0.5
});

this.ballEntity.collision.on('triggerenter', this.ballEntity.script.hitBoxScript.onTriggerEnter, this.ballEntity.script.hitBoxScript);

This worked!

1 Like

It may be, if you’d like submit an issue about it on the engine repo.

Posted it here: https://github.com/playcanvas/engine/issues/2195
Lets see…

1 Like

Had a quick look at this: It sounds like the expected behaviour is that the removal of the rigidbody turns into a trigger type.

For now, I would say the best workaround is to disable and enable the entity which will recreate the collision as a trigger.

2 Likes