Spawning objects and placing them at specific place

okay I have followed parts of this [SOLVED] Spawn entity at certain time

but something is working yet something is not, I have a coin that is place in a specific place in 3D space, I created an empty entity and assigned this code:

var Spawner = pc.createScript('spawner');


Spawner.attributes.add('objectToSpawn', {
    type: 'entity'
});

// initialize code called once per entity
Spawner.prototype.initialize = function() {
    
    
    
    setInterval(()=>{
        console.log('called');
        let clonedObj = this.objectToSpawn.clone();
        console.log(clonedObj);
        let root = this.app.root;
        root.addChild(clonedObj);
        clonedObj.setLocalPosition(this.entity.getLocalPosition().x, this.entity.getLocalPosition.y, this.entity.getLocalPosition.z);
    }, 2000);
    
    
    
    
};


Spawner.prototype.update = function(dt) {
    
};

what I am trying to do is, add that entity that I want to clone as an attribute and then on interval I want to clone that thing and then add it to the root and then change its position, something isn’t working and I don’t know what is it

This line isn’t correct, in two ways:

  • For the Y and Z axis you forgot the () when calling the method, it should be:
this.entity.getLocalPosition().y
  • Even though you are spawning many objects, the position they are placed is the same, so at the end you will only get a single object rendering. You will have to randomize their placement, check this example on how to do that:

https://developer.playcanvas.com/en/tutorials/procedural-levels/

1 Like

ah, I am fucking dumb, it worked the entire code was correct just that (), on the subject of spawning on the same place, I have a code that moves the object as soon as it spawns, the spawning location is special thats why I am using the spawning location which is also the entity’s location. so it spawns in the special place and then it starts moving automatically

1 Like

Good, a small performance optimization in case you are interested, do this instead:

var localPos = this.entity.getLocalPosition();
clonedObj.setLocalPosition(localPos);
1 Like

1 Like