Code Collision

https://playcanvas.com/editor/scene/1678072

Hi guys, I’m trying to set collisions with code, but still not working, can you help me?

var AddMeshCollision = pc.createScript('addMeshCollision');

AddMeshCollision.prototype.initialize = function() {

    // Get the entity on which this script is attached

    var entity = this.entity;

    // Add a small delay to ensure that the collision system is fully initialized

    this.app.once('collision:ready', function () {

        // Create a mesh collision component and add it to the entity

        var collision = entity.addComponent('collision', {

            type: 'mesh'

        });

        // Set the mesh to use the same model as the entity

        collision.model = entity.model;

    });

};

I have to add many models, so I’m trying to do it a easy way.

Hi @Breno_Abner!

What is not working exactly?

There seems to be a problem with the script on the entities, so I suggest to remove the script from the entities and add it again.

Maybe you also need a rigidbody component?

I tryied use riridbody, but my raycast and outliner stops working

You still need a collision component. At the moment I only see a rigidbody component.

Yes, I’m trying to activate de collision component with code. I have to add many objects, rename and activate the collision, so I’m trying to do a shorten path.

like this: GNAE-TRONCO-SUPERIOR - PLAYCANVAS

Yes, but right now there is also no script on the entity to do this?