- Home /
Endless Runner world generation stops at certain point.
Hey, I am making a 2d (Verticle) endless runner game, following a tutorial. Now I ran into a problem that my endless level generation stops when I go to high (when i go 3000 higher from start). It doesn't have anything to do with worldorigin, I think it must be in the script somewhere. Here is a video of what happens zoomed out: https://imgur.com/a/7pA13tN And here is the script: private const float PLAYER_DISTANCE_SPAWN_LEVEL_PART = 250f;
[SerializeField] private List<Transform> levelPartList;
[SerializeField] private Transform levelPart_Start;
[SerializeField] private Player player;
private Vector3 lastEndPosition;
private void Awake()
{
lastEndPosition = levelPart_Start.Find("EndPosition").position;
int startingSpawnLevelParts = 20;
for (int i = 0; i < startingSpawnLevelParts; i++)
{
SpawnLevelPart();
}
}
private void Update()
{
if (Vector3.Distance(player.transform.position, lastEndPosition) < PLAYER_DISTANCE_SPAWN_LEVEL_PART)
{
SpawnLevelPart();
}
}
private void SpawnLevelPart()
{
Transform chosenLevelPart = levelPartList[Random.Range(0, levelPartList.Count)];
Transform lastLevelPartTransform = SpawnLevelPart(chosenLevelPart, lastEndPosition);
lastEndPosition = lastLevelPartTransform.Find("EndPosition").position;
}
private Transform SpawnLevelPart(Transform levelPart, Vector3 spawnPosition)
{
Transform levelPartTransform = Instantiate(levelPart, spawnPosition, Quaternion.identity);
return levelPartTransform;
}
Answer by toficofi · Mar 21, 2021 at 12:54 PM
It's quite hard to tell from the video but it looks like the player slowly "catches up" to the new level parts and eventually overtakes them, in which case Vector3.Distance(player.transform.position, lastEndPosition)
would start to increase again.
I don't know how your player controller looks, but is it possible you're moving the player in FixedUpdate
, in which case it might be running more frequently than your level generation?
Thanks for your help! I'm currently not at my PC but I wil check it out when I'm back.
@toficofi Here is my player script, it doesn't matter if put it in Upate or FixedUpdate.
void FixedUpdate()
{
if (ispressed)
{
thruster.Emit(1); //Emit some particle
RaycastHit2D hit = Physics2D.Raycast(rayOrigin.transform.position, Vector2.down, rayCheckDistance);
if (hit.collider)
{
rb.AddForce(Vector2.up * jump, ForceMode2D.Impulse);
}
}
}
public void OnPointerDown(PointerEventData eventData)
{
ispressed = true;
}
public void OnPointerUp(PointerEventData eventData)
{
ispressed = false;
}
You're using physics to move the player, so that will move in FixedUpdate either way. You could try changing the generator to use FixedUpdate instead, or make the generator create a bunch of level parts at once.
Answer by rh_galaxy · Mar 21, 2021 at 04:04 PM
If you need to spawn more than one levelPart per frame your code will not work.
Try
private void Update()
{
while(Vector3.Distance(player.transform.position, lastEndPosition)<PLAYER_DISTANCE_SPAWN_LEVEL_PART)
{
SpawnLevelPart();
}
}
But Vector3.Distance is maybe also not correct to do, since it will measure the the distance regardless of the player is in front of the level part or behind it, that will cause you to stop adding level parts when too much in front like what is seen in your clip.
@rh_galaxy Thanks for your help! First I could go max at y = 3000 Now it is still not completely infinite I can go around y = 5700, but I don't think anyone can reach that. I cant come up with an explanation why it stops there, really weird, or I'm just stupid...