아카이브
[개발일지] Undead Survivor 2일차 본문
맵 구현
룰타일 생성
타일 적용
하이어라키에서 타일 맵을 만들어 줌
팔레트를 통해 맵타일을 적용해준다
재배치 이벤트
타일맵에 콜라이더 부착
Player에 자식 오브젝트 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.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;
}
}
}
카메라 설정
그림자가 안보이면 타일맵의 레이어를 -1로 고쳐준다
'2D 콘텐츠 제작' 카테고리의 다른 글
2D 프로젝트 개발 발표 (0) | 2023.09.21 |
---|---|
[개발일지] Undead Survivor 5일차 (0) | 2023.09.19 |
[개발일지] Undead Survivor 4일차 (0) | 2023.09.19 |
[개발일지] Undead Survivor 3일차 (0) | 2023.09.15 |
[개발일지] Undead Survivor 1일차 (0) | 2023.09.13 |