using UnityEngine;
public class BackgroundScrolling : MonoBehaviour
{
public GameObject[] backgrounds; // 배경 오브젝트 배열
public float scrollSpeed = 2.0f; // 스크롤 속도
private float backgroundWidth; // 배경의 폭
void Start()
{
// 배경 오브젝트의 폭을 설정
if (backgrounds.Length > 0)
{
backgroundWidth = backgrounds[0].GetComponent<BoxCollider2D>().bounds.size.x;
}
// 배경 오브젝트를 초기 위치에 올바르게 배치
for (int i = 1; i < backgrounds.Length; i++)
{
backgrounds[i].transform.position = new Vector3(
backgrounds[i - 1].transform.position.x + backgroundWidth,
backgrounds[i].transform.position.y,
backgrounds[i].transform.position.z
);
}
}
void Update()
{
// 모든 배경 오브젝트를 왼쪽으로 이동
foreach (GameObject background in backgrounds)
{
background.transform.Translate(Vector3.left * scrollSpeed * Time.deltaTime);
// 배경이 왼쪽 밖으로 나갔는지 확인
if (background.transform.position.x < -backgroundWidth)
{
// 가장 오른쪽 배경의 끝 부분으로 배경을 재배치
RepositionBackground(background);
}
}
}
private void RepositionBackground(GameObject background)
{
// 현재 배경 중 가장 오른쪽에 있는 배경을 찾음
float maxRightX = float.MinValue;
foreach (GameObject bg in backgrounds)
{
if (bg.transform.position.x > maxRightX)
{
maxRightX = bg.transform.position.x;
}
}
// 가장 오른쪽 배경의 끝 부분에 현재 배경을 재배치
Vector3 newPos = new Vector3(maxRightX + backgroundWidth, background.transform.position.y, background.transform.position.z);
background.transform.position = newPos;
}
}
반응형
'메모장' 카테고리의 다른 글
DOTween - SetEase 열거형 (0) | 2024.06.19 |
---|---|
월드 좌표 변환 / 로컬 좌표 변환 (0) | 2024.06.19 |
데이터베이스 프로그래밍 능력단위 평가 (0) | 2024.05.31 |
EditorApplication.delayCall (0) | 2024.05.23 |
게임 인공지능프로그래밍 (0) | 2024.05.13 |