PlayCanvas endless runner: Chunks not spawning immediately after clicking the respawn btn when player has already moved far

1 day ago 1
ARTICLE AD BOX

I have an endless runner game in PlayCanvas. Chunks (level sections) are spawned and recycled based on the player's Z position.

But when the player has driven for about 10 seconds, crashes and respawns → the player appears in empty space for 1–3 seconds before new chunks start spawning.

Relevant code

LevelGenerator.fullReset() (called on ad respawn):

LevelGenerator.prototype.fullReset = function() { console.log("[LevelGenerator] Full Reset"); while (this.activeChunks.length > 0) { var chunk = this.activeChunks.shift(); if (chunk) chunk.destroy(); } for (var i = 0; i < this.chunkPool.length; i++) { this.chunkPool[i].enabled = false; } this.nextSpawnZ = 0; if (this.startTemplate) { this.spawnSpecificChunk(this.startTemplate); } else if (this.chunkPool.length > 0) { this.recycleChunkFromPool(); } var maxInitial = Math.ceil(this.viewDistance / this.chunkSize) + 2; var spawned = this.activeChunks.length; while (this.nextSpawnZ < this.viewDistance && spawned < maxInitial && this.chunkPool.length > 0) { this.recycleChunkFromPool(); spawned++; } console.log("[LevelGenerator] Reset complete – active chunks:", this.activeChunks.length); };

Spawn logic in update():

var playerZ = this.playerEntity.script.chairController.zPos || this.playerEntity.getPosition().z; var threshold = this.nextSpawnZ - this.viewDistance; var spawnCount = 0; while (playerZ > threshold && this.chunkPool.length > 0 && spawnCount < 5) { this.recycleChunkFromPool(); threshold = this.nextSpawnZ - this.viewDistance; spawnCount++; }

Respawn flow (in GameOverManager.respawn()):

this.app.fire('game:resume'); this.app.fire('game:reset'); this.app.fire('game:start'); var chair = this.playerEntity.script.chairController; if (chair) chair.fullReset(); // resets zPos to 0 this.app.fire('game:respawn');

What I already tried:

Increased maxInitial to +15 / +20

Added +200 / +500 / +800 spawn distance in fullReset

Set player zPos = 100 / 150 / 200 in fullReset

Completely refilled chunkPool in fullReset

Increased spawn count limit to 10 / 20 / 30 in update()

Why doesn't the spawn loop in update() trigger right after reset, even though playerZ is reset to ~0? How can I make sure enough chunks are spawned immediately after respawn without changing the player's spawn position?

Read Entire Article