Entity creator help

Hi everyone, just wanna ask if its possible to spawn and delete entity by distance and not timer? been using this script for timer spawn and delete. thanks in advance

EntityCreator.prototype.update = function(dt) {

while (this.entities.length < this.maxCubes) {

    this.spawnCube();

}

// Loop through Entities and delete them when their time is up

for (i = 0; i < this.entities.length; i++) {

    this.entities[i].timer -= dt;

    if (this.entities[i].timer < 0) {

        // entity.destroy() deletes all components and removes Entity from the hierarchy

        this.entities[i].entity.destroy();

        // Remove from the local list

        this.entities.splice(i, 1);

    }

}

};

Hi @Genesis_Miranda Yes you can use distance to determine when to delete an entity, if the distance is below a threshold, you can delete, some pseudocode:

let threshold = 1; 

var v1 = new pc.Vec3(5, 10, 20); // Cube 1 position
var v2 = new pc.Vec3(10, 20, 40); // Cube 2 position
var d = v1.distance(v2);
console.log("The distance between v1 and v2 is: " + d);

if(d < threshold) {
  this.entities[i].entity.destroy();
  this.entities.splice(i, 1);
}
1 Like