unirun1/Assets/Scripts/BackgroundLoop.cs

27 lines
883 B
C#
Raw Normal View History

2026-01-28 14:27:40 +00:00
using UnityEngine;
// 왼쪽 끝으로 이동한 배경을 오른쪽 끝으로 재배치하는 스크립트
public class BackgroundLoop : MonoBehaviour {
private float width; // 배경의 가로 길이
private void Awake() {
2026-01-29 02:32:06 +00:00
BoxCollider2D backgroundCollider = GetComponent<BoxCollider2D>();
width = backgroundCollider.size.x;
//Debug.Log(backgroundCollider.size.x);
2026-01-28 14:27:40 +00:00
// 가로 길이를 측정하는 처리
}
private void Update() {
2026-01-29 02:32:06 +00:00
if(transform.position.x <= -width)
{
Reposition();
}
2026-01-28 14:27:40 +00:00
// 현재 위치가 원점에서 왼쪽으로 width 이상 이동했을때 위치를 리셋
}
// 위치를 리셋하는 메서드
private void Reposition() {
2026-01-29 02:32:06 +00:00
Vector2 offset = new Vector2(width * 2f, 0);
transform.position = (Vector2) transform.position + offset;
2026-01-28 14:27:40 +00:00
}
}