I am making a game where the background moves past the player, and then gets deleted and instantiated outside of the camera to create a parallax effect. The big problem is every so often I'll get gaps in between the background and I don't know how to fix it. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spawner : MonoBehaviour { public GameObject[] stages; public GameObject test; public BoxCollider2D myCollider; public Spikes mySpikes; public static Spawner instance; public void Awake() { instance = this; } // Start is called before the first frame update private void Update() { } // Update is called once per frame public void create() { int rand = Random.Range(0, stages.Length); test = Instantiate(stages[rand], transform.position, Quaternion.identity); } } Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spikes : MonoBehaviour { public float speed; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { transform.Translate(Vector2.left * speed * Time.deltaTime); if(transform.position.x <= -14) { Destroy(gameObject); Spawner.instance.create(); } } }
The way you are doing parallax is super inefficient since you are instantiating all the time. I recommend you to change the sprite position based in the camera movement. I will leave you the script to achieve this. Code (CSharp): using System.Collections.Generic; using UnityEngine; [ExecuteInEditMode] public class ParallaxingScript : MonoBehaviour { public static ParallaxingScript instance; [Header("Backgrounds that move in x and y")] public List<Transform> backgrounds; [Header("Background that only move in x")] public List<Transform> backgroundsX; [SerializeField] private float smoothing = 1f; private float[] parallaxScales; private float[] parallaxScalesX; private Transform cam; private Vector3 previousCamPos; private void Awake() { instance = this; if (Camera.main != null) cam = Camera.main.transform; } private void Start() { previousCamPos = cam.position; } private void Update() { parallaxScales = new float[backgrounds.Count]; for (var i = 0; i < backgrounds.Count; i++) { parallaxScales[i] = backgrounds[i].position.z * -1; } parallaxScalesX = new float[backgroundsX.Count]; for (var i = 0; i < backgroundsX.Count; i++) { parallaxScalesX[i] = backgroundsX[i].position.z * -1; } for (var i = 0; i < backgrounds.Count; i++) { var position = cam.position; var parallaxX = (previousCamPos.x - position.x) * parallaxScales[i]; var parallaxY = (previousCamPos.y - position.y) * parallaxScales[i]; var backgroundTargetPosX = backgrounds[i].position.x + parallaxX; var backgroundTargetPosY = backgrounds[i].position.y + parallaxY; var backgroundTargetPos = new Vector3(backgroundTargetPosX, backgroundTargetPosY, backgrounds[i].position.z); backgrounds[i].position = Vector3.Lerp(backgrounds[i].position, backgroundTargetPos, smoothing * Time.fixedDeltaTime); } for (var i = 0; i < backgroundsX.Count; i++) { var parallaxX = (previousCamPos.x - cam.position.x) * parallaxScalesX[i]; var backgroundTargetPosX = backgroundsX[i].position.x + parallaxX; var backgroundTargetPos = new Vector3(backgroundTargetPosX, backgroundsX[i].position.y, backgroundsX[i].position.z); backgroundsX[i].position = Vector3.Lerp(backgroundsX[i].position, backgroundTargetPos, smoothing * Time.fixedDeltaTime); } previousCamPos = cam.position; } }