[TIL] 24일차 - 팀 과제

김유원·2024년 1월 25일
0

📝 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이 필요할 것 같다.

profile
개발 공부 블로그

0개의 댓글

관련 채용 정보