📝 24.01.25
계속해서 팀 과제를 제작하고 있다.
오늘은 게임 맵을 필요에 따라 생성하는 MapCreator를 만들었다.
MapCreator.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapCreator : MonoBehaviour
{
//TODO: Check basic map's size and make new map's location vector
[SerializeField] List<GameObject> maps;
public static MapCreator I;
private void Awake()
{
I = this;
}
// Start is called before the first frame update
void Start()
{
CreateNewMap();
}
// Update is called once per frame
void Update()
{
}
GameObject CreateNewMap()
{
int rand = Random.Range(0, maps.Count);
GameObject newMap = Instantiate(maps[rand]);
return newMap;
}
public void CreateNextMap(GameObject curMap)
{
GameObject newMap = CreateNewMap();
Map nMap = newMap.GetComponent<Map>();
int newX = nMap.mapX;
int curX = curMap.GetComponent<Map>().mapX;
newMap.transform.position = new Vector3(curMap.transform.position.x + (curX + newX) / 2 + 5, 0, 0);
}
}
가장 핵심이 되는 기능은 CreateNextMap()
인데 새로운 맵을 생성할 때 위치를 잡는 데에 꽤 애를 먹었다. 현재 포지션으로부터 일정한 거리에 맵을 새로 생성하고 싶은데, 각 맵마다 크기가 많이 다르다보니 간격을 유지하기가 어려웠다. 그래서 Map들의 가로 길이를 int mapX
로 저장하여 계산하기로 하였다.
참고로 이 CreateNextMap()
은 특정 워프 위치에 Player가 isTrigger
되었을 때만 실행되도록 제작하였다. 다만, 해당 Tilemap Collider
를 가진 오브젝트를 비활성화 시켜도 여전히 isTrigger
가 작동되어 이를 제어할 bool
이 필요할 것 같다.