Search Unity

Problem with background loop (Endless runner)

Discussion in 'Editor & General Support' started by offerni, Dec 6, 2018.

  1. offerni

    offerni

    Joined:
    Oct 3, 2018
    Posts:
    2
    Hi Guys, I'm trying to make an endless runner but I'm stuck in this problem. For some reason, On transform.translate, one sprite is overlapping the other and creating an unexpected gap on the road, what I'm doing wrong?
    It's my first game ever.
    Thank you so much.
    The values on serializefield are: startX = 6 and endX = -6

    Here's the video,

    and
    Here's my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Road : MonoBehaviour {
    6.  
    7.     [SerializeField] Road road;
    8.     [Range(1f,100f)]
    9.     [SerializeField] float speed = 2f;
    10.     [SerializeField] float endX;
    11.     [SerializeField] float startX;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.        
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.  
    21.         transform.Translate(Vector2.left * speed * Time.deltaTime);
    22.  
    23.         if(transform.position.x <= endX) {
    24.             Vector2 pos = new Vector2(startX, transform.position.y);
    25.             transform.position = pos;
    26.         }
    27.        
    28.     }
    29. }
    30.