Version: Unity 6.0 (6000.0)
언어 : 한국어
파티클 트리거
파티클 시스템 힘 필드 컴포넌트 레퍼런스

파티클 트리거 설정

먼저 파티클이 상호 작용할 수 있는 씬의 콜라이더를 지정합니다. 이렇게 하려면 하나 이상의 콜라이더를 Colliders 리스트 프로퍼티에 할당합니다. 리스트의 콜라이더 수를 늘리려면 콜라이더 리스트 아래의 추가(+) 버튼을 클릭합니다. 리스트에서 콜라이더를 제거하려면 콜라이더를 선택하고 제거(-) 버튼을 클릭합니다. 콜라이더를 리스트의 인덱스에 아직 할당하지 않은 경우 빈 엔트리 오른쪽에 있는 작은 추가(+) 버튼을 사용하여 새 콜라이더를 생성하고 할당할 수 있습니다. 그러면 파티클 시스템의 자식으로 새 게임 오브젝트가 생성되고 스피어 콜라이더를 연결한 다음, 콜라이더를 빈 엔트리에 할당합니다.

콜라이더를 추가한 후 특정 트리거 이벤트 유형을 전달하기 위한 기준을 충족하면 파티클이 수행하는 작업을 지정할 수 있습니다. 파티클이 콜라이더와 상호 작용하는 방식을 설명하는 4가지 이벤트 유형이 있습니다. 이는 다음과 같습니다.

  • Inside: 파티클이 콜라이더의 경계 안에 있습니다.
  • Outside: 파티클이 콜라이더의 경계 밖에 있습니다.
  • Enter: 파티클이 콜라이더의 경계 안으로 들어갑니다.
  • Exit: 파티클이 콜라이더의 경계 밖으로 나갑니다.

인스펙터에는 이러한 이벤트 유형 각각에 대한 드롭다운이 있으며, 이를 통해 파티클이 트리거 이벤트의 조건을 통과할 경우에 수행하는 동작을 선택할 수 있습니다. 이는 다음과 같습니다.

  • Callback: OnParticleTrigger() 콜백 함수에서 파티클에 액세스할 수 있습니다.
  • Kill: 파티클을 파괴합니다. OnParticleTrigger() 콜백 함수에서 파티클에 액세스할 수 없습니다.
  • Ignore: 파티클을 무시합니다. OnParticleTrigger() 콜백 함수에서 파티클에 액세스할 수 없습니다.

OnParticleTrigger() 내에서 파티클에 액세스

트리거 이벤트 중 하나에 대한 반응으로 Callback을 선택하면 첨부된 스크립트에서 이벤트 조건을 충족하는 파티클에 액세스할 수 있습니다. 이렇게 하려면 먼저 첨부된 스크립트에 OnParticleTrigger() 함수를 추가해야 합니다. 이 함수 내에서 ParticlePhysicsExtensions.GetTriggerParticles() 함수를 호출하여 트리거 이벤트의 기준을 충족하는 파티클 리스트를 가져옵니다. 이 함수는 파티클을 가져올 트리거 이벤트(Inside, Outside, Enter, Exit)를 지정하는 ParticleSystemTriggerEventType과 함수가 결과에 채우는 파티클 목록을 가져옵니다. 리스트에서 파티클에 액세스하거나 수정하거나 파괴할 수 있습니다. 또한 함수는 충돌 정보를 출력하는 선택적 파라미터(예: 각 파티클이 트리거한 콜라이더)를 가져올 수도 있습니다. Collider Query Mode 프로퍼티는 이 파라미터를 통해 어떤 정보를 사용할 수 있는지 제어합니다.

API와 API 사용 방법에 대한 자세한 내용은 아래의 예시를 참조하십시오.

예제

콜라이더와의 상호 작용

아래의 예제에서 파티클은 콜라이더 경계 안으로 진입할 때 빨간색으로 변한 다음, 콜라이더의 경계를 벗어날 때 다시 녹색으로 변합니다.

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

[ExecuteInEditMode]
public class TriggerScript : MonoBehaviour
{
    ParticleSystem ps;

    // these lists are used to contain the particles which match
    // the trigger conditions each frame.
    List<ParticleSystem.Particle> enter = new List<ParticleSystem.Particle>();
    List<ParticleSystem.Particle> exit = new List<ParticleSystem.Particle>();

    void OnEnable()
    {
        ps = GetComponent<ParticleSystem>();
    }

    void OnParticleTrigger()
    {
        // get the particles which matched the trigger conditions this frame
        int numEnter = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Enter, enter);
        int numExit = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Exit, exit);

        // iterate through the particles which entered the trigger and make them red
        for (int i = 0; i < numEnter; i++)
        {
            ParticleSystem.Particle p = enter[i];
            p.startColor = new Color32(255, 0, 0, 255);
            enter[i] = p;
        }

        // iterate through the particles which exited the trigger and make them green
        for (int i = 0; i < numExit; i++)
        {
            ParticleSystem.Particle p = exit[i];
            p.startColor = new Color32(0, 255, 0, 255);
            exit[i] = p;
        }

        // re-assign the modified particles back into the particle system
        ps.SetTriggerParticles(ParticleSystemTriggerEventType.Enter, enter);
        ps.SetTriggerParticles(ParticleSystemTriggerEventType.Exit, exit);
    }
}

이 예제의 결과는 아래 이미지를 참조하십시오.

에디터 뷰
에디터 뷰
게임 뷰
게임 뷰

여러 콜라이더와의 상호 작용

다음 예시는 GetTriggerParticles() 함수에서 추출할 수 있는 추가 충돌 데이터를 활용합니다. 이렇게 하면 첫 번째 콜라이더의 경계 안에 있는 파티클이 빨간색으로 변하고, 두 번째 콜라이더의 경계 안에 있는 파티클이 파란색으로 변하거나, 두 콜라이더 내부의 파티클이 녹색으로 변합니다. 또한 파티클이 콜라이더 안에 없으면 파티클이 흰색으로 변합니다. 이 예시에서는 Collider Query ModeAll로 설정되어 있습니다.

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

[ExecuteInEditMode]
public class TriggerScript : MonoBehaviour
{
    void OnParticleTrigger()
    {
        ParticleSystem ps = GetComponent();
 
        // particles
        List inside = new List();
        List exit = new List();
 
        // get
        int numInside = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Inside, inside, out var insideData);
        int numExit = ps.GetTriggerParticles(ParticleSystemTriggerEventType.Exit, exit);
 
        // iterate
        for (int i = 0; i < numInside; i++)
        {
            ParticleSystem.Particle p = inside[i];
            if (insideData.GetColliderCount(i) == 1)
            {
                if (insideData.GetCollider(i, 0) == ps.trigger.GetCollider(0))
                    p.startColor = new Color32(255, 0, 0, 255);
                else
                    p.startColor = new Color32(0, 0, 255, 255);
            }
            else if (insideData.GetColliderCount(i) == 2)
            {
                p.startColor = new Color32(0, 255, 0, 255);
            }
            inside[i] = p;
        }
        for (int i = 0; i < numExit; i++)
        {
            ParticleSystem.Particle p = exit[i];
            p.startColor = new Color32(1, 1, 1, 255);
            exit[i] = p;
        }
 
        // set
        ps.SetTriggerParticles(ParticleSystemTriggerEventType.Inside, inside);
        ps.SetTriggerParticles(ParticleSystemTriggerEventType.Exit, exit);
    }
}

이 예제의 결과는 다음 이미지를 참조하십시오.

게임 뷰
게임 뷰
파티클 트리거
파티클 시스템 힘 필드 컴포넌트 레퍼런스