본문 바로가기

종스크롤 슈팅게임(1942)

7. 원근감 있는 무한 배경 만들기

빈 오브젝트 3개 생성해서 그 안에 같은 스프라이트 오브젝트를 3개씩 만들어줍니다.

왼쪽부터 A, B, C

 

그리고 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는 A 내부 오브젝트를 맵핑

원근감을 위해 A, B, C 스크립트에 있는 Speed를 A가 가장 빠르게, C가 가장느리게 설정해줍니다.

 

결과