using System.Collections; using System.Collections.Generic; using UnityEngine; public class Parallax : MonoBehaviour { [SerializeField] private new GameObject camera; [SerializeField] private float parallaxScale; [SerializeField] private float PixelsPerUnit; private float startPos, length; private void Start() { startPos = transform.position.x; length = GetComponent().bounds.size.x; } private void Update() { float temp = camera.transform.position.x * (1 - parallaxScale); float dist = camera.transform.position.x * parallaxScale; transform.position = Clamp(new Vector2(startPos + dist, transform.position.y), PixelsPerUnit); if (temp > startPos + length / 2) startPos += length; else if (temp < startPos - length / 2) startPos -= length; } private Vector2 Clamp(Vector2 location, float ppu) { return new Vector2(Mathf.CeilToInt(location.x * ppu), Mathf.CeilToInt(location.y * ppu)) / ppu; } }