Skip to content
김선영 edited this page Apr 11, 2025 · 1 revision
using UnityEngine;
using System.Collections;

public class MagicCircleDrawer : MonoBehaviour
{
    public float radius = 5f;        // 마법진 반지름
    public float drawSpeed = 3f;     // 그려지는 속도
    public float rotationSpeed = 10f; // 회전 속도
    public int circleSegments = 36;  // 원의 세그먼트 수
    
    private TrailRenderer trailRenderer;
    private bool isDrawing = false;

    void Start()
    {
        trailRenderer = GetComponent<TrailRenderer>();
        if (trailRenderer == null)
        {
            trailRenderer = gameObject.AddComponent<TrailRenderer>();
        }
        
        // 초기 설정
        trailRenderer.time = 2.0f;
        trailRenderer.startWidth = 0.2f;
        trailRenderer.endWidth = 0.1f;
    }

    public void StartDrawing()
    {
        if (!isDrawing)
        {
            isDrawing = true;
            StartCoroutine(DrawCircle());
        }
    }
    
    IEnumerator DrawCircle()
    {
        // 시작 지점으로 이동 (바로 라인이 나타나지 않도록)
        transform.position = new Vector3(radius, 0, 0);
        trailRenderer.Clear();
        
        // 원을 그리기 위해 각도별로 위치 계산
        float angleStep = 360f / circleSegments;
        float currentAngle = 0;
        
        while (currentAngle <= 360)
        {
            float radians = currentAngle * Mathf.Deg2Rad;
            float x = radius * Mathf.Cos(radians);
            float z = radius * Mathf.Sin(radians);
            
            transform.position = new Vector3(x, 0, z);
            
            // 마법진 회전 효과 추가
            transform.Rotate(0, rotationSpeed * Time.deltaTime, 0);
            
            currentAngle += angleStep * drawSpeed * Time.deltaTime;
            yield return null;
        }
        
        isDrawing = false;
    }
}
using UnityEngine;
using System.Collections;

public class SlimeSummonEffect : MonoBehaviour
{
    [Header("Magic Circle Settings")]
    public MagicCircleDrawer magicCircleDrawer;
    public GameObject magicCircleObject;    // 완성된 마법진 프리팹
    public float circleDrawTime = 2.0f;     // 마법진 그리기 시간
    
    [Header("Slime Settings")]
    public GameObject slimePrefab;          // 소환할 슬라임 프리팹
    public float slimeAppearTime = 0.5f;    // 슬라임 등장 시간
    public float slimeJumpHeight = 2.0f;    // 슬라임 점프 높이
    
    [Header("VFX")]
    public ParticleSystem magicParticles;   // 마법 파티클
    public Light magicLight;                // 마법 조명

    private GameObject slimeInstance;

    public void StartSummon()
    {
        StartCoroutine(SummonSequence());
    }
    
    IEnumerator SummonSequence()
    {
        // 1. 마법진 그리기
        magicCircleDrawer.StartDrawing();
        
        // 마법 조명 효과
        if (magicLight != null)
        {
            magicLight.enabled = true;
            StartCoroutine(PulseLight(magicLight, circleDrawTime));
        }
        
        yield return new WaitForSeconds(circleDrawTime);
        
        // 2. 완성된 마법진 표시
        if (magicCircleObject != null)
        {
            magicCircleObject.SetActive(true);
            magicCircleObject.transform.position = transform.position;
        }
        
        // 3. 마법 파티클 재생
        if (magicParticles != null)
        {
            magicParticles.Play();
        }
        
        // 4. 슬라임 소환
        Vector3 spawnPosition = transform.position;
        spawnPosition.y -= 1f; // 바닥 아래에서 시작
        
        slimeInstance = Instantiate(slimePrefab, spawnPosition, Quaternion.identity);
        
        // 5. 슬라임 점프 애니메이션
        StartCoroutine(SlimeJumpAnimation(slimeInstance.transform, slimeAppearTime, slimeJumpHeight));
        
        yield return new WaitForSeconds(slimeAppearTime + 0.5f);
        
        // 6. 마법진 페이드아웃 (선택 사항)
        if (magicCircleObject != null)
        {
            StartCoroutine(FadeOutObject(magicCircleObject, 1.0f));
        }
    }
    
    IEnumerator PulseLight(Light light, float duration)
    {
        float startIntensity = light.intensity;
        float time = 0;
        
        while (time < duration)
        {
            float pulseIntensity = startIntensity + Mathf.Sin(time * 5f) * startIntensity * 0.5f;
            light.intensity = pulseIntensity;
            time += Time.deltaTime;
            yield return null;
        }
        
        light.intensity = startIntensity;
    }
    
    IEnumerator SlimeJumpAnimation(Transform slimeTransform, float duration, float height)
    {
        Vector3 startPos = slimeTransform.position;
        Vector3 endPos = startPos;
        endPos.y += height;
        
        float time = 0;
        
        // 위로 점프
        while (time < duration * 0.5f)
        {
            float t = time / (duration * 0.5f);
            slimeTransform.position = Vector3.Lerp(startPos, endPos, t);
            time += Time.deltaTime;
            yield return null;
        }
        
        // 착지 후 튕김 효과
        float bounceTime = 0;
        float bounceHeight = height * 0.3f;
        
        while (bounceTime < duration * 0.5f)
        {
            float t = bounceTime / (duration * 0.5f);
            float yOffset = Mathf.Sin(t * Mathf.PI) * bounceHeight;
            slimeTransform.position = new Vector3(endPos.x, endPos.y - bounceHeight + yOffset, endPos.z);
            bounceTime += Time.deltaTime;
            yield return null;
        }
        
        slimeTransform.position = endPos;
    }
    
    IEnumerator FadeOutObject(GameObject obj, float duration)
    {
        Renderer[] renderers = obj.GetComponentsInChildren<Renderer>();
        float time = 0;
        
        // 모든 머티리얼의 초기 알파값 저장
        float[][] initialAlphas = new float[renderers.Length][];
        for (int i = 0; i < renderers.Length; i++)
        {
            initialAlphas[i] = new float[renderers[i].materials.Length];
            for (int j = 0; j < renderers[i].materials.Length; j++)
            {
                Color c = renderers[i].materials[j].color;
                initialAlphas[i][j] = c.a;
            }
        }
        
        while (time < duration)
        {
            float t = time / duration;
            
            // 모든 렌더러의 모든 머티리얼 알파값 조정
            for (int i = 0; i < renderers.Length; i++)
            {
                for (int j = 0; j < renderers[i].materials.Length; j++)
                {
                    Color c = renderers[i].materials[j].color;
                    c.a = initialAlphas[i][j] * (1 - t);
                    renderers[i].materials[j].color = c;
                }
            }
            
            time += Time.deltaTime;
            yield return null;
        }
        
        obj.SetActive(false);
    }
}

Clone this wiki locally