Hi
I’m making an endless runner and for the sections(tiles or…) that spawn back to back, I’m using box collider to pool the next section and it works fine.
Except when I increase the speed there appears a gap between sections, and even sometimes the gaps are not the same.
You’re not precise enough with your position calculation, somehow a drift or offset gets into it and creates the gaps.
It’s not clear from your code, is the SectionSpawner game object moving? How do you calculate spawnOffset?
Try to manually calculate the proper offsets and confirm there’s no gaps if you place the pieces manually in the editor, then compare them to the value calculated in your script.
The sections that spawn are the ones moving towards the camera(player)
And I calculated the spawnOffset by moving the section until no more gaps were there and then adding that value to the offset
Which works fine but as I increase the speed it gets buggy
That doesn’t explain much. You’ll have to post more code to show exactly how the position of the pieces is calculated.
One possible explanation is that you’re not taking the actual position of the last piece into account. Whenever FixedUpdate runs, the pieces will be mid-movement and not align nicely with the player. When speed is low, the random offset will be small but it’ll get bigger with increased speed. You need to apply the offset not to a fixed position but to the position of the last piece.
There isn’t actually much code to it
I measured it manually
The section has a box collider which triggers a spawn
and the offset that I manually insert spawns the section with the offset towards the z axis
and everytime the player passes the collider on the section it spawns the next one
Maybe it’s something about collider on trigger not responding fast enough?
I’m new to programming so I don’t know what works best
This is the code that moves the sections maybe this is wrong idk
also I use an object pooler script from Brackeys maybe that could be it
using UnityEngine;
public class SectionMove : MonoBehaviour
{
// Speed at which the object will move
public float speed = 5f;
void Update()
{
// Move the object negatively along the Z-axis
transform.Translate(Vector3.back * speed * Time.deltaTime);
}
}
So I assume SectionSpawner is stationery with the camera and doesn’t move?
And you enter spawnOffset manually and it’s the same for all sections?
Which would confirm what I explained before: Collisions aren’t exact, the physics engine updates at a fixed interval and there will be a varying amount of overlap between colliders. Basically from almost no overlap to however much the two colliders moved in relation to each other in that frame, how much exactly is practically random. And the faster the two colliders move relative to each other, the bigger the potential overlap will be.
What you need to do, is to not spawn the new sections based on the non-moving position of the SectionSpawner. Instead use the position of the last spawned section, and apply the offset to that. By making the new section’s position relative to the last, its exact position when the trigger fires doesn’t matter anymore.
Thank you this makes a lot of sense
How would I approach this
I mean how do I use the position of the last spawned section
As I said I’m a beginner that’s why I ask
Also yes the SectionSpawner and the camera are stationary
The SpawnFromPool method should return the spawned section, keep a reference to it in the spawner, e.g. lastSection.
The first time, lastSection will be null, so position the first section based on the spawner position like you’re doing now. But for subsequent sections, use lastSection.transform instead of transform (the spawner’s transform) to calculate the position.