0
\$\begingroup\$

I'm trying to create in Unity an "Endless Runner" game, where the player remains stationary on the 0 of the axes while the tiles underneath him scroll with a fixed speed on the Z-Axis.
The script attached spawns 5 tiles at the start while already scrolling at the speed set in the GameManager and then keeps on spawning tiles based on a timer.

The issue I'm facing is the following:

The tiles that are spawned with the timer are too much distant from the starting set of tiles.

How can I calculate the correct distance between the set of the already scrolling tiles and those the script start to spawn after 5 seconds in order to have seamless scrolling?

I'm attaching the script with what I've written so far in the hope that someone knows how to proceed, or perhaps knows a different method. Thanks!

public class TileManager : MonoBehaviour
{
    [SerializeField] private Transform player;
    [SerializeField] private GameObject[] tilesArray;
    [SerializeField] private int tilesToSpawn = 5;
    [SerializeField] private float zPosSpawn = 0f;
    [SerializeField] private float tileLength = 20f;

    public List<GameObject> tilesList;
    private float spawnRate;
    private float timeSinceLastSpawned;

    void Start()
    {
        //scrollingSpeed is -5
        spawnRate = GameManager.Instance.scrollingObjectsSpeed * -1;
        StartSpawnLogic();
    }


    void Update()
    {
        timeSinceLastSpawned += Time.deltaTime;
        SpawnTimer();
        DestroyAfterSpawn();
        
    }

    
    private void StartSpawnLogic()
        {
            for (int i = 0; i < tilesToSpawn; i++)
            {
                if (i == 0)
                {
                    SpawnTiles(0);
                }
                else
                {
                    SpawnTiles(Random.Range(0, tilesArray.Length));
                }
            }
        }

    //SPAWN FUNCTION
    private void SpawnTiles(int tileIndex)
    {
        //On Instantiation a list is filled to later dispose of the tiles after they reached a certain point
        tilesList.Add((GameObject)Instantiate(tilesArray[tileIndex], transform.forward * zPosSpawn, Quaternion.identity));
        zPosSpawn += tileLength;
    }

    //TIMER
    private void SpawnTimer()
    {
        if (timeSinceLastSpawned >= spawnRate)
        {
            timeSinceLastSpawned = 0;
            SpawnTiles(Random.Range(1, tilesArray.Length));
        }
    }

    void RemoveObjFromList(GameObject tile)
    {
        Destroy(tile);
        tilesList.Remove(tile);
    }

    private void DestroyAfterSpawn()
    {
        for (int i = 0; i < tilesList.Count; i++)
        {
            if (tilesList[i].transform.position.z <= -20f)
            {
                RemoveObjFromList(tilesList[i]);
            }
        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Your solution is a lot more complicated than it needs to be. The dependencies on GameManager, speed, TileManager's transform, hard coded values, spawn-rate, and timing are all unnecessary. I suggest taking a look a this free tutorial published by Unity: learn.unity.com/project/unit-3-sound-and-effects \$\endgroup\$
    – Jim U
    Dec 24 '20 at 1:46
  • \$\begingroup\$ Thanks Jim, your answer helped me to understand that I don't need to spawn the whole tiles but only the obstacles while keeping just one tile \$\endgroup\$
    – Luckydraw
    Dec 25 '20 at 14:41
0
\$\begingroup\$

There are a few ways to do this, here is one way. All you need to do is move the transform.position of gameobject attached to the background sprite.

at a set speed at Time.DeldaTime, something like this (in Update()):

transform.position.x -= speed * Time.DeltaTime 

Then add an if loop to catch when the bounds of the background reach a point. This is an example, basically - when the background reaches said point - change the transform.position to equal a fixed position on the other side of the screen.

if(tranfom.position <= -50x){
    transform.position = new Vector2(50, 0, 0,)
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.