Endless runner issue

Hey guys , so i have an issue with endless runner , so i have a prefab of map which is instantiated when player touches the trigger.
So the thing is , i don’t know how to make it spawn correctly , i tried taking position of first instantiated prefab and add it’s length, but , map itself moves , so it changes it’s x position every second , also i have “gas pedal” button (when pressed ,map’s speed movement increases), and because of that i have a gap between instantiated maps
Usually gap’s size differ ( because of speed) , here’s my code:

distance = Instantiated.transform.position.x ;
void OnTriggerEnter(Collider other)
if (other.tag == “Player”)
Instantiated = Instantiate(swapped, new Vector3(distance + 90, 0, 0), Quaternion.identity) as GameObject;


Any type of help would be really appreciated!

Just thinking off the top of my head, so its just a thought as im not at pc to have a test.

Try putting your distance inside of the OnTriggerEnter. Might have something to do with the timestep difference between framerate and physics step calls.

It depends where you are finding the distance value and what has passed before using it.
Probably better to put it in that collision call since it will all be done at the same timestep.

See how it goes.

1 Like

Hey, thanks for your reply. I put my distance in OntriggerEnter void , it works, but still there’s a gap , as i said previously because of the gas pedal (speed constantly changes), i was thinking about creating a loop , which is going to subtract prefab’s x position, until there’s no gap, but i don’t know how to put it together and make it work, if you have any suggestions , i would really appreciate.

Couple of questions and thoughts.

Where is the trigger volume? Is it a part of the prefab?

Can u pop a sort of pseudo code of the mechanic/process of please.

How are you moving the road prefab? Are u translating it?

Hard to tell, so just popping a few thoughts into the mix :slight_smile:

1 Like

Thanks for reply again , trigger is a part of prefab!

Here’s my code for map movement :

void Update()
speed = speedButt.GetComponent().speed;
transform.Translate(-speed, 0f, 0f);

And there’s a button code (gas pedal) :

void addspeed()
if (map.GetComponent().dontGo != 0)
if (ispressed)
if (speed <= 1.0f)
speed += 0.01f;
else if (!ispressed)
speed -= 0.01f;

I did a similar thing in 2d yesterday and also got gaps. I “solved” it by introducing a 10% overlap (spawning slightly early) which seemed to be enough to compensate for the variance.


I am new here, and I dont know the lesson yet (where is it? Unity-course?)

But the problem with the collider reminds me on the “ball-boncing-higher”-problem at block-breaker.

@arthymon did you use “discrete” or “continous” collosion-detecting?



1 Like

Hey , thanks for reply @ninjachimp , could you please explain more about 10% overlap

Hey @phrobion , thanks for reply . It’s not really one of the courses , just my little project. if i understood you correctly , i didn’t really use collision , i used OntriggerEnter , when my “player” touches the trigger , it’s instantiates a prefab.

Sure, keep in mind I hacked this before bed last night and I haven’t tested it over a long run to see if problems creep in, but it seems to work ok in the editor.

using UnityEngine;

namespace NinjaChimpStudios.FreezoRun.Scripts.Game {

    public class TiledSpriteSpawner : MonoBehaviour {

        public TiledSprite TheTiledSprite;
        public float Overlap = 1.0f;

        private float _spriteDeltaTime;
        private float _lastSpawn;

        void Start() {
            _spriteDeltaTime = TheTiledSprite.GetComponent<SpriteRenderer>().bounds.size.x / TheTiledSprite.Speed * Overlap;

        void Update() {
            if ((Time.time - _lastSpawn) > _spriteDeltaTime) {
                Instantiate(TheTiledSprite, transform.position, Quaternion.identity);
                _lastSpawn = Time.time;


So this is my generic tiled sprite spawner, it basically instantiates sprites at an appropriate interval so they are tiled as they move (for seamelss moving parallax backgrounds). The spawn rate depends on the speed (configured in the sprite prefab) but as I was getting occasional gaps of a pixel or two I introduced the configurable overlap. I set it to 0.9 in the editor, which makes the new sprite spawn 10% earler. Hope it helps.

1 Like

You could create an empty gameObject at the beginning of the track GameObject Prefab and another one at the end of it and use it as reference points when instantiating


Today I learned sth in here about unity.
Confused me, but seems legit.
(Your question made me to quest the following post)

If you got a trigger, you got a collider.
In your case one collider is that “wall”.
I assume that rigidbody is the car (player) .
So: Does your player use “discrete” or “continous” collosion-detecting?

I hope for a win-win-situation.
Your problem solved. My question answered. :smiley:



1 Like

I guess you have a rigidbody attached to the car, whereas that trigger collider doesn’t.

Try to set the collision detection setting of the car to Continuous and see if the gap decreases/vanishes or it doesn’t change anything.


Hey , yes you’re right , my player have rigidbody with discrete collision detecting , unfortunately changing it to continuous doesn’t fix the problem, although the gap decreases it’s size, but i’ll try something else , thanks anyway!

Hey, thanks for reply . I changed my collision detecting to continuous , the gap decreases it’s size, but as i said it’s hard to check , because the size of the gap is always different , as speed constantly changes

Hey , thanks for reply. I actually was thinking about that, i’ll give it a go !

1 Like

Thanks for the info , i am going to try something out with your code , i’ll write to you after i’m done

Yep , it worked , thanks a lot !


Thanks everyone for the support , problem solved.

1 Like

I wanted to try a different approach, and tested it rapidly in 2D and it worked. I’m curious now if it could work on OP project too.

[code]public class CollScript : MonoBehaviour {

public Transform PlayerTransform;
    public GameObject SpriteToInstantiate;
    public Transform ParentTransform;
    private Vector3 _spawnPosition;

    void OnTriggerEnter2D (Collider2D coll) {
        if(coll.transform == PlayerTransform) {
            _spawnPosition = 2 * transform.position - transform.parent.position;
            GameObject newTile = Instantiate(SpriteToInstantiate, _spawnPosition, ParentTransform.rotation, ParentTransform);
            newTile.name = SpriteToInstantiate.name;


I tried to avoid, in my code, any use of specific offsets and relied on the fact that the collider that triggers the spawning of the new sprite is always in a specific known position, and used it to calculate the spawning point of the new sprite.

Infos about the variables used:

  • PlayerTransform is the player transform used to check if the collider is triggered
  • SpriteToInstantiate, self explanatory
  • ParentTransform is the Transform of the Map container game object (pic below)

It works without problems even with the collision detection set to Discrete and a 0.02 Fixed Update frequency, up until ungodly movement speeds ( 100 * Time.deltaTime works, at 200* I need to increase the Fixed Update frequency).

Edit: Simplified and corrected the code

1 Like

Privacy & Terms