Endless Parallax Background using System.Collections; using System.Collections.Generic; using UnityEngine; [ExecuteInEditMode] public class EndlessParallaxBackground : MonoBehaviour { public ParallaxCamera parallaxCamera; List parallaxLayers = new List(); public float viewZone = 5; // The distance at which the layers are repositioned private float layerWidth; private float totalWidth; void Start() { if (parallaxCamera == null) parallaxCamera = Camera.main.GetComponent(); if (parallaxCamera != null) parallaxCamera.onCameraTranslate += Move; SetLayers(); } void SetLayers() { parallaxLayers.Clear(); for (int i = 0; i < transform.childCount; i++) { EndlessParallaxLayer layer = transform.GetChild(i).GetComponent(); if (layer != null) { layer.name = "Layer-" + i; parallaxLayers.Add(layer); } } if (parallaxLayers.Count > 0) { layerWidth = parallaxLayers[0].GetLayerWidth(); totalWidth = layerWidth * parallaxLayers.Count; } } void Move(float delta) { foreach (EndlessParallaxLayer layer in parallaxLayers) { layer.Move(delta); // Check if the layer needs to be repositioned if (layer.transform.position.x < Camera.main.transform.position.x - viewZone) { Vector3 newPos = layer.transform.position; newPos.x += totalWidth; layer.transform.positi // Reorder layers to maintain continuity layer.transform.SetAsLastSibling(); } } } } EndlessParallaxLayer: using System.Collections; using System.Collections.Generic; using UnityEngine; [ExecuteInEditMode] public class EndlessParallaxLayer : MonoBehaviour { public float parallaxFactor; private float layerWidth; private void Start() { // Assuming the layer is a sprite renderer layerWidth = GetComponent().bounds.size.x; } public void Move(float delta) { Vector3 newPos = transform.localPosition; newPos.x -= delta * parallaxFactor; transform.localPosition = newPos; } public float GetLayerWidth() { return layerWidth; } public bool IsOutOfView(float viewZone) { return transform.position.x < Camera.main.transform.position.x - viewZone; } } ParallaxCamera: using System.Collections; using System.Collections.Generic; using UnityEngine; [ExecuteInEditMode] public class ParallaxCamera : MonoBehaviour { public delegate void ParallaxCameraDelegate(float deltaMovement); public ParallaxCameraDelegate onCameraTranslate; private float oldPosition; void Start() { oldPositi } void Update() { if (transform.position.x != oldPosition) { if (onCameraTranslate != null) { float delta = oldPosition - transform.position.x; onCameraTranslate(delta); } oldPositi } } }