GameDevelop/Unity팀프로젝트

Unity | 적 스폰 전 애니메이션 코드 구현

도도돋치 2025. 7. 29. 21:05
Contents 접기
728x90

오늘은 적이 등장할 때 멋진 이펙트(폭발, 소환 등) 가 먼저 나오고, 그 뒤에 적이 생성되도록 만드는 간단한 시스템을 구현했다.

 

 목표

  • 적 등장 전에 이펙트 프리팹을 먼저 보여주고
  • 적을 그 자리에서 자연스럽게 생성
  • 코루틴 없이 간단한 구조로 처리

 

구조 설명

이전에 만들어 두었던 EnemySpawnManager에서 적 스폰 코드 전 이펙트 실행 함수를 추가하였다.

 

EnemySpawnManager의 구조:

기능 설명
SpawnFromParent() 부모 오브젝트 아래에 있는 스폰 포인트들을 찾아 적을 생성
PlaySpawnEffect() 이펙트를 먼저 생성한 뒤 자동 제거 (0.5초 후)

 

 

코드

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

public class EnemySpawnManager : MonoBehaviour
{
    [SerializeField] private GameObject _spawnEffectPrefab;

    public List<GameObject> SpawnFromParent(Transform parent, Transform container = null, Action onCompleted = null)
    {
        List<GameObject> spawned = new List<GameObject>();

        if (parent == null)
            return spawned;

        EnemySpawnPoint[] points = parent.GetComponentsInChildren<EnemySpawnPoint>();

        foreach (var point in points)
        {
            if (point.enemyPrefabs == null || point.enemyPrefabs.Count == 0) continue;

            foreach (var prefab in point.enemyPrefabs)
            {
                if (prefab == null) continue;

                Vector3 spawnPos = point.transform.position;

                PlaySpawnEffect(spawnPos);

                GameObject enemy = Instantiate(prefab, spawnPos, Quaternion.identity, container);
                spawned.Add(enemy);
            }
        }

        onCompleted?.Invoke();
        return spawned;
    }

    private void PlaySpawnEffect(Vector3 position)
    {
        if (_spawnEffectPrefab == null) return;

        GameObject fx = Instantiate(_spawnEffectPrefab, position, Quaternion.identity);
        Destroy(fx, 0.5f); 
    }
}

 

 

사용법

  1. EnemySpawnPoint에는 적 프리팹들을 리스트로 넣어준다.
  2. SpawnEffectPrefab에는 폭발, 소환 같은 이펙트 프리팹을 넣는다.
  3. SpawnFromParent()를 호출하면 자동으로:
    • 이펙트가 생성되고
    • 그 다음 적이 생성된다.

 

728x90