게임오브젝트 3개를 생성해서 각각 배경을 넣어주는데

order in layer로 계층구조를 잡아줄것이다

a부터 c까지 각각 -3, -4, -5

게임 오브젝트의 좌표는 모두 0 0 0

복사를 해서 각각 3개로 만들어주고

0의 Y좌표는 0

1의 Y좌표는 -10

2의 Y좌표는 10

카메라는 그대로

배경은 뒤로 옮겨 눈속임을 할것이다.

 

스크립트 생성후 배경이 움직이는 로직 작성

using System.Collections;
using System.Collections.Generic;
using TreeEditor;
using UnityEditor;
using UnityEngine;

public class Background : MonoBehaviour
{
    public float speed;
    void Start()
    {
        
    }

    void Update()
    {
        Vector3 curPos = transform.position;
        Vector3 nextPos = Vector3.down * speed * Time.deltaTime;
        transform.position = curPos + nextPos;
    }
}

그런데 이렇게 하면 맨뒤에 아무것도 없는 배경이 보인다.

따라서 배경을 무한으로 만들어 주어야한다.

 

카메라의 시점

카메라를 잡을때 메인카메라의 시점 * 2를해서 화면이 짤리기전에 한번 더 보여주게  하였고

y좌표가 -1일때 시작점을 바꾸어 연속적으로 보이게 하였다.

using System.Collections;
using System.Collections.Generic;
using TreeEditor;
using UnityEditor;
using UnityEngine;

public class Background : MonoBehaviour
{
    public float speed;
    public int startIndex;
    public int endIndex;
    public Transform[] sprites;

    float viewHeight;
    
    void Start()
    {
        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))
        {
            //#.Sprite ReUse
            Vector3 backSpritePos = sprites[startIndex].localPosition;
            Vector3 frontSpritePos = sprites[endIndex].localPosition;

            sprites[endIndex].transform.localPosition = backSpritePos + Vector3.up * viewHeight;

            //이동이 완료되면 EndIndex, StartIndex 갱신
            //#.Cursor Index Change
            int startIndexSave = startIndex;
            startIndex = endIndex;
            endIndex = (startIndexSave -1 == -1) ? sprites.Length-1 : startIndexSave-1;
        }

    }
}

A, B, C를 나눈 이유는 원근감을 주기 위해서이다.

 

Parallax: 거리에 따른 상대적 속도를 활용한 기술

C는 속도1 B는2 A는4

 

using System.Collections;
using System.Collections.Generic;
using TreeEditor;
using UnityEditor;
using UnityEngine;

public class Background : MonoBehaviour
{
    public float speed;
    public int startIndex;
    public int endIndex;
    public Transform[] sprites;

    float viewHeight;
    
    void Start()
    {
        viewHeight = Camera.main.orthographicSize * 2; // 카메라
    }

    void Update()
    {
        

        if (sprites[endIndex].position.y < viewHeight * (-1))
        {
            Move();
            Scrolling();
        }

        void Move()
        {
            Vector3 curPos = transform.position;
            Vector3 nextPos = Vector3.down * speed * Time.deltaTime;
            transform.position = curPos + nextPos;
        }

        void Scrolling()
        {
            //#.Sprite ReUse
            Vector3 backSpritePos = sprites[startIndex].localPosition;
            Vector3 frontSpritePos = sprites[endIndex].localPosition;

            sprites[endIndex].transform.localPosition = backSpritePos + Vector3.up * viewHeight;

            //이동이 완료되면 EndIndex, StartIndex 갱신
            //#.Cursor Index Change
            int startIndexSave = startIndex;
            startIndex = endIndex;
            endIndex = (startIndexSave - 1 == -1) ? sprites.Length - 1 : startIndexSave - 1;
        }
    }
}

'산대특' 카테고리의 다른 글

2D Vertical Shooting Game 모작 최종  (0) 2024.04.08
팬텀로즈스칼렛 모작 최종  (0) 2024.04.08
2D Airplane - 5  (1) 2024.03.14
2D Airplane - 4  (0) 2024.03.12
2D Airplane - 3  (0) 2024.03.12

+ Recent posts