상세 컨텐츠

본문 제목

캐릭터 오브젝트 주변에 원 퍼져 나가게 하기(스캔)

유니티/게임만들기

by MJ_119 2024. 5. 27. 01:24

본문

스캔할때처럼 캐릭터 중앙에서부터 원을 그려서 캐릭터 반경 5m 주변까지 원이 점차 커져나가는 것을 구현

 

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

public class Scan : MonoBehaviour
{
    public LineRenderer lineRenderer;
    public float radius = 0f; // Initial radius
    public int segments = 360; // Number of segments to draw the circle
    public float expandRate = 1f; // Expansion rate per second
    public float maxRadius = 5f; // Maximum radius
    public Transform characterTransform; // Reference to the character's transform

    void Start()
    {
        lineRenderer = GetComponent<LineRenderer>();
        StartCoroutine(DrawCircleRoutine());
    }

    void Update()
    {
        // 빈 오브젝트의 위치를 캐릭터의 위치로 업데이트
        if (characterTransform != null)
        {
            transform.position = characterTransform.position;
        }
    }

    IEnumerator DrawCircleRoutine()
    {
        while (true) // Infinite loop
        {
            radius += expandRate * Time.deltaTime; // Expand the radius every frame
            if (radius > maxRadius)
            {
                radius = 0f; // Reset the radius to 0 if it exceeds the maximum
            }
            DrawCircleWithRadius(radius);
            yield return null; // Wait until the next frame
        }
    }

    void DrawCircleWithRadius(float radius)
    {
        lineRenderer.positionCount = segments + 1;
        lineRenderer.startColor = Color.red;
        lineRenderer.endColor = Color.red;

        float x;
        float y = transform.position.y; // Use the y position of the object
        float z;

        float angle = 20f;

        for (int i = 0; i < (segments + 1); i++)
        {
            x = Mathf.Sin(Mathf.Deg2Rad * angle) * radius + transform.position.x; // Add the object's x position
            z = Mathf.Cos(Mathf.Deg2Rad * angle) * radius + transform.position.z; // Add the object's z position

            lineRenderer.SetPosition(i, new Vector3(x, y, z));

            angle += (360f / segments);
        }
    }
}

 

캐릭터 오브젝트 자식에 빈 오브젝트를 만들고 거기에 스크립트를 넣어줌

그뒤 라인렌더러와 캐릭터 트랜스폼에 각각 오브젝트를 넣어주면 됨.

 

 

관련글 더보기