

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class Background : MonoBehaviour { public float speed; public int startIndex; public int endIndex; public Transform[] sprites; float viewHeight; void Awake() { viewHeight = Camera.main.orthographicSize * 2; } void Update() { Vector3 curPos = transform.position; Vector3 nextPos = Vector3.down * speed * Time.deltaTime; transform.position = curPos + nextPos; if(sprites[endIndex].position.y < viewHeight * (-1)) { Vector3 backSpritePos = sprites[startIndex].localPosition; sprites[endIndex].transform.localPosition = backSpritePos + Vector3.up * viewHeight; int startIndexSave = startIndex; startIndex = endIndex; endIndex = startIndexSave - 1 == -1 ? sprites.Length : startIndexSave - 1; } } } | cs |
