[SOLVED] Create a triggers grid

Hello, i want programmatically create a triggers grid, let’s say 20x20, but i have no idea how to do it…can anyone help me or give me some hint? Thanks

I just created this script:

pc.script.attribute('width', 'number', 10);
pc.script.attribute('depth', 'number', 10);

pc.script.create('triggergrid', function (app) {
    // Creates a new Triggergrid instance
    var Triggergrid = function (entity) {
        this.entity = entity;
    };

    Triggergrid.prototype = {
        // Called once after all resources are loaded and before the first update
        initialize: function () {
            var triggerEnter = function (entity) {
                console.log(entity.name + ' entered trigger ' + this.entity.name);
            };

            for (var x = 0; x < this.width; x++) {
                for (var z = 0; z < this.depth; z++) {
                    var e = new pc.Entity();
                    e.name = 'Trigger_' + x + '_' + z;
                    e.addComponent('collision', {
                        halfExtents: new pc.Vec3(0.5, 0.5, 0.5)
                    });
                    e.collision.on('triggerenter', triggerEnter);
                    e.setPosition(x, 0, z);
                    app.scene.root.addChild(e);
                }
            }
        },

        // Called every frame, dt is time in seconds since last update
        update: function (dt) {
        }
    };

    return Triggergrid;
});

Seems to work OK.

wow! that’s great, thanks a lot Will. I try this right away!

1 Like

Hi Will, i have tryed to implement your code in my map but doesn’t seems to work, maybe i’m doing something wrong, but since u have given a name to every trigger it should display the name of the trigger when the raycast hit it and it doesn’t.
Here is the link to my project.
https://playcanvas.com/editor/scene/397361

I don’t think trigger volumes are returned by raycasts. Raycasts only hit rigid body components, and triggers don’t have a rigid body.

hehe :stuck_out_tongue: told u i missed something lol … is there a way to make the trigger visible? so i can check the position?

In game or Editor?
In Editor just selecting it will render primitive of mesh of collision component.
In game you would need to make some rendering code, and renderLine will be very handy here (this is what used to render gizmos in Editor).