当粒子击中碰撞体时,将调用 OnParticleCollision。
可用于在被粒子击中时对 GameObject 施加伤害。
该消息将发送到附加到粒子系统的脚本和被击中的 Collider。
当从附加到 GameObject(该 GameObject 具有 Collider)的脚本调用 OnParticleCollision 时,
GameObject 参数表示 ParticleSystem。在任何给定帧内,
对于与其发生碰撞的每个粒子系统,Collider 最多只收到一条消息,
即使粒子系统在当前帧中使用多个粒子击中该 Collider 也是如此。
要检索 ParticleSystem 造成的所有碰撞的详细信息,
必须使用 ParticlePhysicsExtensions.GetCollisionEvents
检索 ParticleSystem.CollisionEvent 的数组。
当从附加到 ParticleSystem 的脚本调用 OnParticleCollision 时,
GameObject 参数表示附加了被 ParticleSystem 击中的 Collider 的 GameObject。
对于每个被击中的 Collider,
ParticleSystem 最多收到一条消息。
如上所述,必须使用 ParticlePhysicsExtensions.GetCollisionEvents 来检索 GameObject 上的所有碰撞事件。
仅当在 Particle System Collision 模块的 Inspector 中启用了 Send Collision Messages
时,才发送消息。
OnParticleCollision 可以作为协同程序使用 - 在函数中使用 yield 语句即可。
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class ExampleClass : MonoBehaviour { public ParticleSystem part; public List<ParticleCollisionEvent> collisionEvents;
void Start() { part = GetComponent<ParticleSystem>(); collisionEvents = new List<ParticleCollisionEvent>(); }
void OnParticleCollision(GameObject other) { 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++; } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.