GameDevelop/Unity팀프로젝트

Unity 2D 게임 개발일지 – 카메라 전환 & 맵 이동 + 트러블슈팅

도도돋치 2025. 7. 3. 15:47
Contents 접기
728x90

오늘은 Unity에서 맵 클리어 시 플레이어를 다음 구역으로 이동하고  화면 전환(Fade In/Out)을 구현했다.
진행하면서 발생한 문제들도 함께 기록했다.


 1. 목표 기능

  • 몬스터를 모두 처치하면 다음 구역으로 이동
  • 씬을 새로 로드하지 않고 같은 씬에서 플레이어 위치만 변경
  • 카메라는 플레이어를 계속 따라가도록 유지
  • 화면은 페이드 아웃/인으로 부드럽게 전환

 

2. 구현 흐름

WaveSpawner에 다음 맵 이동 로직 추가

몬스터를 모두 처치했을 때 호출되는 OnAllWavesCleared()에서 다음 맵으로 이동 코루틴을 실행하도록 했다.

private void OnAllWavesCleared() 
{ 
     Debug.Log("맵 클리어, 다음맵으로 이동"); 
     triggered = false; 
     StartCoroutine(MoveToNextMap()); 
}

 

Fade In/Out 처리

ScreenFader 스크립트를 작성하여 CanvasGroup의 알파값으로 페이드 처리:

public class ScreenFader : MonoBehaviour
{
    public CanvasGroup canvasGroup;
    public float fadeDuration = 1f;

    public IEnumerator FadeOut()
    {
        float t = 0;
        while (t < fadeDuration)
        {
            t += Time.deltaTime;
            canvasGroup.alpha = Mathf.Lerp(0, 1, t / fadeDuration);
            yield return null;
        }
        canvasGroup.alpha = 1;
    }

    public IEnumerator FadeIn()
    {
        float t = 0;
        while (t < fadeDuration)
        {
            t += Time.deltaTime;
            canvasGroup.alpha = Mathf.Lerp(1, 0, t / fadeDuration);
            yield return null;
        }
        canvasGroup.alpha = 0;
    }
}
 

주의사항

  • 처음에 FadeImage를 꺼두지 않고, alpha = 0으로 시작해야 한다.
  • canvasGroup은 Inspector에서 꼭 할당해야한다.

 

실제 이동 코루틴

페이드 아웃 후 플레이어를 다음 SpawnPoint로 옮기고 페이드 인:

 
private IEnumerator MoveToNextMap()
{
    yield return StartCoroutine(fader.FadeOut());

    GameObject player = GameObject.FindWithTag("Player");
    Transform spawnPoint = GameObject.Find("PlayerSpawnPoint2-1").transform;

    if (player != null && spawnPoint != null)
    {
        player.transform.position = spawnPoint.position;
    }

    yield return new WaitForSeconds(0.2f);

    yield return StartCoroutine(fader.FadeIn());
}

 

 

3. 트러블슈팅

문제1: Fade가 동작하지 않음

 

원인
ScreenFader의 canvasGroup에 할당이 빠져있음.

 

에러 메시지

UnassignedReferenceException: The variable canvasGroup of ScreenFader has not been assigned.

 

해결

  • Hierarchy에서 FadeImage에 CanvasGroup을 드래그해서 할당
  • 또는 Awake()에서 자동으로 GetComponent 추가
void Awake()
{
    if (canvasGroup == null)
        canvasGroup = GetComponent<CanvasGroup>();
}
 

 

문제2: 맵 클리어 로그가 무한 반복

 

원인
Update()에서 aliveChecker.AreAllDead()가 계속 true로 남아 무한 호출.

 

해결
OnAllWavesCleared()에서 triggered = false;를 추가하여 루프 중단:

private void OnAllWavesCleared()
{
    Debug.Log("맵 클리어, 다음맵으로 이동");
    triggered = false;
    StartCoroutine(MoveToNextMap());
}

 

 

문제3: FadeImage 처음 상태

 

오해
FadeImage를 SetActive(false)로 꺼둬야 한다고 생각함.

 

해결
오브젝트는 활성화 상태로 두고 CanvasGroup.alpha = 0으로 투명 처리해야 동작함

 

 

4. 작업 요약

✅ 플레이어 SpawnPoint 이동으로 구역 전환 구현
✅ Cinemachine 카메라는 플레이어 계속 따라감
✅ CanvasGroup으로 Fade 처리
✅ 무한 반복 및 NullReference 이슈 해결

728x90