빈 오브젝트 3개 생성해서 그 안에 같은 스프라이트 오브젝트를 3개씩 만들어줍니다.
그리고 A, B, C 각각의 내부 오브젝트들을 Y축 위치를 위부터 -10, 0, 10 순서로 입력해줍니다.
그리고 A, B, C가 겹쳐서 화면에 보일 수 있게 Order in Layer을 A가 가장높게, C가 가장 낮게 설정해줍니다.
Background 스크립트를 만들어 A, B, C 오브젝트에 넣어줍니다.
Background 스크립트 로직
public float speed;
public int startIndex;
public int endIndex;
public Transform[] sprites; //내부 오브젝트 맵핑
float viewHeight;
void Awake()
{
viewHeight = Camera.main.orthographicSize * 2; //카메라 사이즈
}
void Update()
{
move();
scrolling();
}
void move()
{
Vector3 curPos = transform.position;
Vector3 nextPost = Vector3.down * speed * Time.deltaTime;
transform.position = curPos + nextPost;
}
void scrolling()
{
if (sprites[endIndex].position.y < viewHeight * (-1))
{
Vector3 topSpritePos = sprites[startIndex].localPosition; //맨 위 칸
Vector3 bottomSpritePos = sprites[endIndex].localPosition; //맨 아래 칸
sprites[endIndex].transform.localPosition = topSpritePos + Vector3.up * viewHeight;
int startIndexSave = startIndex; //맨 윗칸 인덱스번호 임시 저장
startIndex = endIndex; //맨 아래 칸 인덱스 번호 -> 맨 위칸 인덱스번호
endIndex = startIndexSave - 1 == -1 ? sprites.Length - 1 : startIndexSave - 1; //맨 위칸 인덱스번호 -1 -> 맨 아래칸 인덱스 번호
}
}
맵핑
원근감을 위해 A, B, C 스크립트에 있는 Speed를 A가 가장 빠르게, C가 가장느리게 설정해줍니다.
결과
'종스크롤 슈팅게임(1942)' 카테고리의 다른 글
9. 텍스트파일을 이용한 커스텀 배치 구현 (0) | 2022.01.25 |
---|---|
8. 오브젝트 풀링 (0) | 2022.01.25 |
6. 아이템과 필살기 구현하기 (0) | 2022.01.20 |
5. 간단한 UI 만들기 (0) | 2022.01.19 |
4. 적 전투기 총알 & 플레이어 피격 이벤트 구현 (0) | 2022.01.18 |