[Unity] 종 스크롤 슈팅 게임 7 (배경)

펑크린·2021년 9월 14일

Unity

목록 보기
10/13

00. 배경 준비

  • 준비된 sprite를 빈 오브젝트를 만들어 위와 같이 배치한다.

01. Script

  • 배경이 아래로 내려오면서 앞으로 나간다는 느낌을 구현한다.
  • 준비된 3개의 sprite로 무한한 배경을 구현하기 위해 배경이 카메라 범위를 벗어나면 가장 위에 위치한 배경 앞으로 자리를 옮긴다.

Scrolling 기법

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

Parallax 기법

  • 각 배경 그룹의 속도를 달리한다.
  • 가까이 보이는 배경일 수록 speed를 빠르게 조절.

02. 결과

profile
코 익 인 간

0개의 댓글