我将如何获取粒子碰撞的网格过滤器,然后将该网格分配给粒子。
所以我有一个OnParticleCollision。我击中该对象,并获得其网格过滤器。我不希望将其分配给我的粒子,以使其发挥其物理构造的效果。
到目前为止是我的代码。
void Start()
{
Debug.Log("Script Starting...");
part = GetComponent<ParticleSystem>();
collisionEvents = new List<ParticleCollisionEvent>();
}
void OnParticleCollision(GameObject coll)
{
// Getting the object of the collider
Collider obj = coll.GetComponent<Collider>();
Mesh mesh = obj.GetComponent<MeshFilter>().mesh;
// Assign the mesh shape of the collider to that of the particle
ElectricWave.shape.meshRenderer = mesh; // I know this doesnt work as is.
// Play effect
ElectricWave.Play();
}
如果您希望系统中的所有粒子都采用该网格,那很简单。只需获取与对象碰撞的网格并将其应用于ParticleSystemRenderer
:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParticleCollision : MonoBehaviour
{
private ParticleSystemRenderer particleRenderer;
void Start()
{
particleRenderer = GetComponent<ParticleSystemRenderer>();
}
void OnParticleCollision(GameObject other)
{
particleRenderer.renderMode = ParticleSystemRenderMode.Mesh;
particleRenderer.material = other.GetComponent<Renderer>().material;
}
}
但是,如果您只是想更改仅该粒子的网格,则会更加复杂,因为ParticleCollisionEvent数据不包含哪个粒子与之碰撞。一个好的起点可能是查看ParticleCollisionEvent.intersection值,然后尝试使用GetParticles查找最接近该点的粒子。不过,这可能在计算上极其昂贵。