아카이브

[개발일지] Undead Survivor 2일차 본문

2D 콘텐츠 제작

[개발일지] Undead Survivor 2일차

timbercat 2023. 9. 14. 18:21

맵 구현

룰타일 생성

타일 적용

하이어라키에서 타일 맵을 만들어 줌

팔레트를 통해 맵타일을 적용해준다

재배치 이벤트

타일맵에 콜라이더 부착

컴포짓 콜라이더를 넣은 후 타일맵 콜라이더 used by composite을 체크, Is Trigger를 체크 체크하지 않으면 플레이어와 타일맵의 충돌이 일어난다
바디 타입을 Static으로 설정
태그 추가 후 설정

Player에 자식 오브젝트 Area 추가

Area에 박스콜라이더 2d를 넣고 크기를 타일맵과 동일하게 만든다, Is Trigger 체크, 태그 Area로 설정

게임매니저 오브젝트를 만들고 스크립트 부착

GameManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    public static GameManager instance; //static: 정적으로 사용하겠다는 키워드. 바로 메모리에 얹어버림
    public Player player;

    void Awake()
    {
        //Awake 생명주기에서 인스턴스 변수를 자기자신 this로 초기화
        instance = this;
    }
}

타일맵에 Reposition 스크립트 부착, 복사하여 4개 생성

Reposition.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem.Switch;

public class Reposition : MonoBehaviour
{
    void OnTriggerExit2D(Collider2D collision)
    {
        if (!collision.CompareTag("Area")) //Area태그가 아니면 실행하지 않는다
            return;

        //거리를 구하기 위해 플레이어 위치와 타일맵 위치를 미리 저장
        Vector3 playerPos = GameManager.instance.player.transform.position;
        Vector3 myPos = transform.position;
        //X축과 Y축 각각의 거리, 플레이어 위치 - 타일맵 위치
        float diffX = Mathf.Abs(playerPos.x - myPos.x); //Mathf.Abs(): 절대값 함수
        float diffY = Mathf.Abs(playerPos.y - myPos.y);

        Vector3 playerDir = GameManager.instance.player.inputVec;
        //3항 연산자 (조건) ? (true일 때 값) : (false일 때 값)
        float dirX = playerDir.x < 0 ? -1 : 1;
        float dirY = playerDir.y < 0 ? -1 : 1;
        
        //switch ~ case: 값의 상태에 따라 로직을 나눠주는 키워드
        switch (transform.tag)
        {
            case "Ground":
                if(diffX > diffY) //두 오브젝트 거리 사이에서, x축이 y축보다 크면 수평이동
                {
                    transform.Translate(Vector3.right * dirX * 40);
                } 
                else if (diffX < diffY) //x축이 y축보다 작으면 수직이동
                {
                    transform.Translate(Vector3.up * dirY * 40);
                }
                break;
            case "Enemy":

                break;
        }
    } 
}

카메라 설정

Pixel Perfect Camera 컴포넌트를 추가하고 설정
패키지 매니저에서 Cinemachine 설치
버추얼카메라를 만든 뒤 Follow에 Player 넣기
메인카메라 Update Method 설정을 Fixed Update로 바꿈

그림자가 안보이면 타일맵의 레이어를 -1로 고쳐준다