Copying sample objects

https://launch.playcanvas.com/1257975?debug=true&ministats=true

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

I want the objects that occur in this scene to occur randomly, but the same scenes always occur. how can i fix this

Kopylama kodları

var TileSpawner = pc.createScript('tileSpawner');
TileSpawner.attributes.add('Tile1', { type: 'entity' });
// initialize code called once per entity
TileSpawner.prototype.initialize = function() {

    this.lastTile = new pc.Vec3(0,-0.25,0);
    //this.app.on('tile:spawn', function (x) {
    for(var i = 0;i<5;i++)
    {
        // 500 tane parkur olıştursun.
        // Onur burda zaman ver aniden çıksın yani bekletme hızla çıkar

     this.entity.findByName('parkur-2').enabled=false;
     this.entity.findByName('parkur-3').enabled=false;
     this.entity.findByName('parkur-4').enabled=false;
     this.entity.findByName('parkur-5').enabled=false;
     this.entity.findByName('parkur-6').enabled=false;
     this.entity.findByName('parkur-7').enabled=false;

     var rand = Math.floor(Math.random() * 5) +1;
     console.log(rand);
    
    if(rand == 1){
        
         this.entity.findByName('parkur-2').enabled=true;
        
    }else if(rand == 2){
        
         this.entity.findByName('parkur-3').enabled=true;
    }
    else if(rand == 3){
        
         this.entity.findByName('parkur-4').enabled=true;
    }
    else if(rand == 4){
        
         this.entity.findByName('parkur-5').enabled=true;
    }
    else if(rand == 5){
        
         this.entity.findByName('parkur-6').enabled=true;
    }
    else if(rand == 6){
        
         this.entity.findByName('parkur-7').enabled=true;
    }
    }
    //},this);
};

// update code called every frame
TileSpawner.prototype.update = function(dt) {
    this.camera = this.app.root.findByName('Camera');   
        var up = new pc.Vec3(-40,-10,0);
        var mid = new pc.Vec3(-40,-10,0);
        var down = new pc.Vec3(-40,-10,0);
        var position = [up, down,mid,mid,mid,mid, mid];
        var rand = position[Math.floor(Math.random() * position.length)];
        this.lastTile = this.lastTile.add(rand);
        this.spawn(this.lastTile);
    //this.relax();

};
TileSpawner.prototype.spawn = function(position){
    // Create an asteroid at (x,y) drifting towards (direction) with (speed) 
    var tile = this.Tile1.clone();
    tile.name = "newTile";
    
    this.app.root.addChild(tile);
    
    tile.enabled = true;
    tile.rigidbody.teleport(position);
    return tile;
};

TileSpawner.prototype.relax = function()
{
    var player = this.app.root.findByName('Player');
    var newTile = this.app.root.findByName('newTile');
    if(newTile.getPosition().z>=player.getPosition().z+20)
        newTile.destroy();
};


Hi @Ozden_Delibas,

From what I see rand returns random numbers correctly on each run. But your logic doesn’t really use that random number to provide different placement.

You basically enable all pieces and leave them rest on the same place.

You should use that random number to update their position based on the logic of your game. Check this example on how it procedurally generates and randomly places entities:

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

3 Likes