How would I spawn an entity every 20 seconds or 30 seconds, also how would I spawn 1 single cloned entity at a certain dt time? I tried to do if(this.timer >3) this.clone teleport 1,1,1
but this spammed the entity every frame bs abuse there’s nothing stopping it
Don’t forget to set the timer on 0 again.
That and also try using a flag variable to achieve this. Should solve it.
whats a flag variable, could you give an example? and i haven’t tried to reset the timer yet
The following code should help you understand.
let flag = false;
if (/*condition is true*/ && flag === false) {
//do whatever
flag = true;
}
1 Like
You’d do something like:
var Spawner = pc.createScript('spawner');
Spawner.attributes.add('interval', { type: 'number', default: 1 });
Spawner.attributes.add('entityToSpawn', { type: 'entity' });
// initialize code called once per entity
Spawner.prototype.initialize = function() {
this.timer = 0;
};
// update code called every frame
Spawner.prototype.update = function(dt) {
this.timer += dt;
if (this.timer >= this.interval) {
var clone = this.entityToSpawn.clone();
this.app.root.addChild(clone);
this.timer = 0;
}
};
2 Likes
Thanks guys, @Leonidas helped me solve it using this method
var Spawner = pc.createScript('spawner');
// initialize code called once per entity
Spawner.prototype.initialize = function() {
this.Spawner = 0;
this.isSpawnerActive = false;
};
Spawner.prototype.beginSpawner = function(){
this.isSpawnerActive = true;
};
// update code called every frame
Spawner.prototype.update = function(dt) {
this.Spawner += dt;
if (this.Spawner > 5 && this.isSpawnerActive === false) {
this.beginSpawner();
var Monster1 = this.app.root.findByName('Monster1');
var F = Monster1.clone();
Monster1.parent.addChild(F);
F.rigidbody.teleport(-12, 1, -4);
}
};
1 Like
This is what I meant. In this code, this.isSpawnerActive
is your flag. This should work perfectly.
1 Like