Hi everyone hope you are all well, so I am working on a 2d space shooter game and I am a bit stuck here. I have created a system where asteroid waves spawn randomly but for some reason all of the enemies seems to move towards the same x-axis position. Here is my code
GameController.prototype.initialize = function () {
const self = this;
const waitForSeconds = (seconds) =>
new Promise((resolve) => setTimeout(resolve, seconds * 1000));
const spawnWaves = async () => {
await waitForSeconds(self.startWait);
while (true) {
for (let i = 0; i < this.hazardCount; i++) {
const hazards = this.app.root.findByTag("hazard");
const newHazard =
hazards[Math.floor(pc.math.random(0, hazards.length))].clone();
const spawnPosition = new pc.Vec3(
pc.math.random(-this.spawnValues.x, this.spawnValues.x),
this.spawnValues.y,
this.spawnValues.z
);
newHazard.rigidbody.teleport(spawnPosition);
newHazard.enabled = true;
await waitForSeconds(self.spawnWait);
}
await waitForSeconds(self.waveWait);
}
};
spawnWaves();
};
The values for attributes are
const gameController = new pc.Entity("GameController");
gameController.addComponent("script");
gameController.script.create(GameController, {
attributes: {
spawnValues: new pc.Vec3(pc.math.random(6, -6), 5, 0),
hazardCount: pc.math.random(3, 7),
spawnWait: 0.5,
waveWait: 4,
startWait: 2,
},
});
app.root.addChild(gameController);
And here is the code that moves the asteroids
Mover.attributes.add("speed", {
type: "number",
default: 1,
});
// initialize code called once per entity
Mover.prototype.initialize = function (dt) {
// Create a vec3 to hold the lerped position
this.lerpedPosition = new pc.Vec3();
// How fast the entity will reach the target
this.speed = 0.3;
};
// update code called every frame
Mover.prototype.update = function (dt) {
this.targetPosition = new pc.Vec3(this.entity.getPosition().x, -6, 0);
// Lerp the current position and the target position
this.lerpedPosition.lerp(
this.entity.getPosition(),
this.targetPosition,
this.speed * dt
);
// Update the entity's position to the lerped position
this.entity.setPosition(this.lerpedPosition);
//console.log(this.targetPosition);
if (this.entity.getPosition.y >= -6) {
this.entity.destroy();
}
};
the asteroids also seems to get slow before reaching the target position wonder why??.