粒子系统碰撞检测
MonoBehaviour.OnParticleCollision(GameObject)
Description
OnParticleCollision is called when a particle hits a collider.
This can be used to apply damage to a game object when hit by particles
粒子碰撞了collider 非Trigger 就会触发 绑定了该脚本的 OnParticleCollision函数,
1.
同一帧中即使一个粒子系统的多个粒子对象均与碰撞体对象发生碰撞 ,碰撞体对象也只会接收到一条碰撞消息,也就是OnXXX只会被调用一次。
绑定在Cube上,那么将会接收到blacksmoke redsomke的碰撞消息 ,并且参数的GameObject也是这2者
void OnParticleCollision(GameObject obj) { Debug.Log(obj.name); }
如果绑定在blackXXX 那么消息函数 OnXXX 的参数GameObject就会是Cube 和redXXXX
2.
还可以利用ParticleCollisionEvent 数组来处理 多个粒子对象 碰撞事件事件-官方实例
using UnityEngine; using System.Collections; public class ExampleClass : MonoBehaviour { public ParticleSystem part; public ParticleCollisionEvent[] collisionEvents; void Start() { part = GetComponent<ParticleSystem>(); collisionEvents = new ParticleCollisionEvent[16]; } void OnParticleCollision(GameObject other) { int safeLength = part.safeCollisionEventSize; if (collisionEvents.Length < safeLength) collisionEvents = new ParticleCollisionEvent[safeLength]; int numCollisionEvents = part.GetCollisionEvents(other, collisionEvents); Rigidbody rb = other.GetComponent<Rigidbody>(); int i = 0; while (i < numCollisionEvents) { if (rb) { Vector3 pos = collisionEvents[i].intersection; Vector3 force = collisionEvents[i].velocity * 10; rb.AddForce(force); } i++; } } }