How do I move multiple entities with one script?


#1

So I have several moving platforms in my game and if I try giving each of them a their own script the game starts lagging. so I tried making a singular script for all of them. I gave the blocks number tags and tried both just set the "find by tag " with number and number turned to string

I did the following:

var Translate = pc.createScript('translate');

// initialize code called once per entity
Translate.prototype.initialize = function() {

for(var j = 0; j < 10; j++){
  var broj= j.toString();
    var blocks = this.app.root.findByTag(broj);
    this.startingPositions = [];
    this.startingRotations = [];

    for(var i = 0; i < blocks.length; i++){
        this.startingPositions.push(blocks[i].getLocalPosition().clone());
        this.startingRotations.push(blocks[i].getLocalRotation().clone());
    }
}
    
        
};
var br= 1;
// update code called every frame
    Translate.prototype.update = function(dt) {
                var rnd= pc.math.random(0.0001, br/100);
    var base = -0.05;
    var add =base-rnd; 
      this.entity.translateLocal(0,add,0);
      var poz = this.entity.getPosition().y;
      
  
   
    if(poz <-26){
       
 for(var j = 0; j < 10; j++){
     var broj= j.toString();
     
    var blocks = this.app.root.findByTag(broj);
        for(var i = 0; i < blocks.length; i++){
                blocks[i].rigidbody.teleport(this.startingPositions[i],this.startingRotations[i]);
                blocks[i].rigidbody.linearVelocity = pc.Vec3.ZERO;
                blocks[i].rigidbody.angularVelocity = pc.Vec3.ZERO;
                br=br+1;
        }
       
       
       
    }
    }
};

// swap method called for script hot-reloading
// inherit your script state here
// Translate.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

Does anyone have any suggestion on what I could try? it would be greatly appreciated

also here’s the entire project
https://playcanvas.com/project/591390/overview/keep-climbing

EDIT: I tried using FindByTag(“xyz” || “zyx”) but it also didn’t work


#2

A few things could be the issue in terms of performance and logic:

  • findByTag is pretty expensive to call. If the number of platforms don’t change, I would do findByTag once on initialisation and store the list. That should be a lot faster
  • Using teleport every frame on every block could be expensive. Something to profile for
  • In your initialize function, you are overwriting the startPositions and startingRotations of each iteration of j. This means that objects are teleporting on top of each other and the physics engine has to resolve the collisions which could be expensive too.

#3

any examples I could look up? Sorry I’m actually quite bad at coding and looking up references is my lifeline.
My main problem is getting the blocks to keep looping


#4

No direct examples unfortunately.

You mentioned that the game was slowing down when the platforms had their own script. What was the script doing and how many platforms did you have?


#5

9 platforms. the script determined the speed of their descend and teleported them back after a certain point


#6

That shouldn’t be a problem :thinking:

Here’s an example with many more moving platforms and that runs ok on my phone https://developer.playcanvas.com/en/tutorials/creating-rigid-bodies-in-code/


#7

so it could be just my sub par laptop that is causing troubles


#8

Do you still have access to an older version of the project with the old code?