unity3d粒子碰撞检测,判断例子碰撞到物体

1.首先 物体必须有Collder

2.把特效的Particle System 中的Collision组件里的Send Collision Messages 的对勾打上,才能使函数生效.

脚本

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

public class TeXiaoPengZhuang : MonoBehaviour
{
    private ParticleSystem m_ParticleSystem;

    private void Start()
    {
        m_ParticleSystem = GetComponent();
    }


    private void OnParticleCollision(GameObject other)
    {
        Debug.Log("碰撞到了other="+ other.name);
        if (other.tag == "tt")
        {
            Debug.Log("碰撞到了tt");
        }
    }
}

3.还可以利用ParticleCollisionEvent数组来处理多个粒子对象碰撞事件事件

官方案例(手动在Scene 面板拖动特效或者物体,使他们相碰撞,即可触发OnParticleCollision 函数)

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

public class TeXiaoPengZhuang : MonoBehaviour
{

    public ParticleSystem part;
    public List collisionEvents;

    private void Start()
    {
        part = GetComponent();
        collisionEvents = new List();
    }


    private void OnParticleCollision(GameObject other)
    {
        int numCollisionEvents = part.GetCollisionEvents(other, collisionEvents);
        Rigidbody rb = other.GetComponent();
        int i = 0;
        while (i < numCollisionEvents)
        {
            if (rb)
            {
                Vector3 pos = collisionEvents[i].intersection;
                Vector3 force = collisionEvents[i].velocity * 10;
                rb.AddForce(force);
            }
            i++;
        }

    }
}

你可能感兴趣的:(Unity,C#,unity,粒子特效)