[英]How to detect which particle collided with a rigid body in Unity?
可以说,刚体正在穿过一堆属于单个粒子系统的粒子,并且您希望与刚体碰撞的每个粒子都弹回。 你会怎么做?
当刚体与粒子系统碰撞时,将调用void OnParticleCollision(GameObject other)。 但是,我需要知道粒子系统中的哪个粒子与人体碰撞。
有什么想法吗 ?
使用ParticleSystem.GetParticles
函数,您可以捕获所有“活动”粒子,然后为它们分配索引(您应该从粒子类继承,因为粒子类没有任何索引或id变量)。
GertParticles:
https://docs.unity3d.com/ScriptReference/ParticleSystem.GetParticles.html
您如何访问粒子系统的各个粒子?: https : //answers.unity.com/questions/639816/how-do-you-access-the-individual-particles-of-a-pa.html
如我所说,粒子没有任何标识它们的ID,我知道这似乎不是“最佳方法”,但可以从Unity文档中有关SetCustomParticleData函数( https://docs.unity3d.com/ScriptReference/ParticleSystem的示例)进行查找。 SetCustomParticleData.html ),然后对所有这些元素进行迭代。
同样在同一页面上,您可以看到一个示例, 为每个粒子出生时分配唯一的ID:
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class ExampleClass : MonoBehaviour {
private ParticleSystem ps;
private List<Vector4> customData = new List<Vector4>();
private int uniqueID;
void Start() {
ps = GetComponent<ParticleSystem>();
}
void Update() {
ps.GetCustomParticleData(customData, ParticleSystemCustomData.Custom1);
for (int i = 0; i < customData.Count; i++)
{
// set custom data to the next ID, if it is in the default 0 state
if (customData[i].x == 0.0f)
{
customData[i] = new Vector4(++uniqueID, 0, 0, 0);
}
}
ps.SetCustomParticleData(customData, ParticleSystemCustomData.Custom1);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.