private Transform m_Transform;
private ParticleSystem m_ParticleSystem;
private ParticleSystem.Particle[] m_particles;
public Transform target_Trans; //目标位置.(手动拖拽)
private Vector3 pos; //粒子移动的目标位置.
void Start()
{
m_Transform = gameObject.GetComponent<Transform>();
m_ParticleSystem = gameObject.GetComponent<ParticleSystem>();
m_particles = new ParticleSystem.Particle[m_ParticleSystem.main.maxParticles]; //实例化,个数为粒子系统设置的最大粒子数.
//ParticleSystem.MainModule main = m_ParticleSystem.main;
//main.simulationSpace = ParticleSystemSimulationSpace.Custom; //设置模拟空间为定制.
//main.customSimulationSpace = target_Trans; //定制的模拟空间为目标位置Transform.
pos = m_Transform.InverseTransformPoint(target_Trans.position); //粒子系统模拟空间设置为Local时,需要把目标位置换成粒子系统的本地坐标.
//pos = target_Trans.position; //粒子系统模拟空间为World时,直接把目标位置赋值给pos.
}
void Update()
{
//获取当前激活的粒子.
int num = m_ParticleSystem.GetParticles(m_particles);
//设置粒子移动.
for (int i = 0; i < num; i++)
{
//m_particles[i].position = Vector3.Lerp(m_particles[i].position, Vector3.zero, 0.1f);
m_particles[i].position = Vector3.Lerp(m_particles[i].position, pos, 0.05f);
}
//重新赋值粒子.
m_ParticleSystem.SetParticles(m_particles, num);
}
private Transform m_Transform;
private ParticleSystem m_ParticleSystem;
private ParticleSystem.Particle[] m_particles;
public Transform target_Trans; //目标位置.(手动拖拽)
private Vector3 pos; //粒子移动的目标位置.
void Start()
{
m_Transform = gameObject.GetComponent<Transform>();
m_ParticleSystem = gameObject.GetComponent<ParticleSystem>();
m_particles = new ParticleSystem.Particle[m_ParticleSystem.main.maxParticles]; //实例化,个数为粒子系统设置的最大粒子数.
//ParticleSystem.MainModule main = m_ParticleSystem.main;
//main.simulationSpace = ParticleSystemSimulationSpace.Custom; //设置模拟空间为定制.
//main.customSimulationSpace = target_Trans; //定制的模拟空间为目标位置Transform.
//pos = m_Transform.InverseTransformPoint(target_Trans.position); //粒子系统模拟空间设置为Local时,需要把目标位置换成粒子系统的本地坐标.
pos = target_Trans.position; //粒子系统模拟空间为World时,直接把目标位置赋值给pos.
}
void Update()
{
//获取当前激活的粒子.
int num = m_ParticleSystem.GetParticles(m_particles);
//设置粒子移动.
for (int i = 0; i < num; i++)
{
//m_particles[i].position = Vector3.Lerp(m_particles[i].position, Vector3.zero, 0.1f);
m_particles[i].position = Vector3.Lerp(m_particles[i].position, pos, 0.05f);
}
//重新赋值粒子.
m_ParticleSystem.SetParticles(m_particles, num);
}
这里代码设置了粒子系统的定制模拟空间为目标点,设置粒子移动时,直接移动到零点即可.
private Transform m_Transform;
private ParticleSystem m_ParticleSystem;
private ParticleSystem.Particle[] m_particles;
public Transform target_Trans; //目标位置.(手动拖拽)
private Vector3 pos; //粒子移动的目标位置.
void Start()
{
m_Transform = gameObject.GetComponent<Transform>();
m_ParticleSystem = gameObject.GetComponent<ParticleSystem>();
m_particles = new ParticleSystem.Particle[m_ParticleSystem.main.maxParticles]; //实例化,个数为粒子系统设置的最大粒子数.
ParticleSystem.MainModule main = m_ParticleSystem.main;
main.simulationSpace = ParticleSystemSimulationSpace.Custom; //设置模拟空间为定制.
main.customSimulationSpace = target_Trans; //定制的模拟空间为目标位置Transform.
//pos = m_Transform.InverseTransformPoint(target_Trans.position); //粒子系统模拟空间设置为Local时,需要把目标位置换成粒子系统的本地坐标.
//pos = target_Trans.position; //粒子系统模拟空间为World时,直接把目标位置赋值给pos.
}
void Update()
{
//获取当前激活的粒子.
int num = m_ParticleSystem.GetParticles(m_particles);
//设置粒子移动.
for (int i = 0; i < num; i++)
{
m_particles[i].position = Vector3.Lerp(m_particles[i].position, Vector3.zero, 0.1f);
//m_particles[i].position = Vector3.Lerp(m_particles[i].position, pos, 0.05f);
}
//重新赋值粒子.
m_ParticleSystem.SetParticles(m_particles, num);
}
private Transform m_Transform;
private ParticleSystem m_ParticleSystem;
private ParticleSystem.Particle[] m_Particles;
[Header("边缘路径点父物体")]
public Transform targetParent; //路径点父物体.
private List<Vector3> targetList; //路径点集合.
[Header("粒子移动速度.")]
[Range(0,8)]
public float speed = 0.5f; //粒子移动速度.
void Start () {
m_Transform = gameObject.GetComponent<Transform>();
m_ParticleSystem = gameObject.GetComponent<ParticleSystem>();
m_Particles = new ParticleSystem.Particle[m_ParticleSystem.main.maxParticles];
targetList = new List<Vector3>();
Transform[] target_TransArray = targetParent.GetComponentsInChildren<Transform>();
for (int i = 1; i < target_TransArray.Length; i++)
{
targetList.Add(target_TransArray[i].position);
}
}
void Update () {
//获取当前激活的粒子.
int num = m_ParticleSystem.GetParticles(m_Particles);
//通过设置粒子的持续时间startLifetime和设置移动速度speed,即可使粒子能在时间内走完准确的路径.
//设置粒子移动.
for (int i = 0; i < num; i++)
{
//计算所用的生命时间与总周期时间的比例.
float proportion = (m_Particles[i].startLifetime - m_Particles[i].remainingLifetime) / m_Particles[i].startLifetime;
//根据比例,计算所在路径点的位置.继而设置方向.
int index = Mathf.FloorToInt(proportion * targetList.Count);
if (index >= 0 && index < targetList.Count - 1)
{
//设置粒子移动的方向.
//Vector3 direction = targetList[index + 1] - targetList[index]; //模拟空间为World.
Vector3 direction = m_Transform.InverseTransformPoint(targetList[index + 1]) - m_Transform.InverseTransformPoint(targetList[index]); //模拟空间为Local.
//设置移动的速度.
m_Particles[i].velocity = direction * (1.0f / speed) * (1.0f / m_Transform.localScale.x);
}
else
{
//超出路径之后,粒子销毁.
m_Particles[i].remainingLifetime = 0;
}
}
//重新赋值粒子.
m_ParticleSystem.SetParticles(m_Particles, num);
}
private Transform m_Transform;
private ParticleSystem m_ParticleSystem;
private ParticleSystem.Particle[] m_Particles;
[Header("边缘路径点父物体")]
public Transform targetParent; //路径点父物体.
private List<Vector3> targetList; //路径点集合.
[Header("粒子移动速度.")]
[Range(0,8)]
public float speed = 0.5f; //粒子移动速度.
void Start () {
m_Transform = gameObject.GetComponent<Transform>();
m_ParticleSystem = gameObject.GetComponent<ParticleSystem>();
m_Particles = new ParticleSystem.Particle[m_ParticleSystem.main.maxParticles];
targetList = new List<Vector3>();
Transform[] target_TransArray = targetParent.GetComponentsInChildren<Transform>();
for (int i = 1; i < target_TransArray.Length; i++)
{
targetList.Add(target_TransArray[i].position);
}
}
void Update () {
//获取当前激活的粒子.
int num = m_ParticleSystem.GetParticles(m_Particles);
//通过设置粒子的持续时间startLifetime和设置移动速度speed,即可使粒子能在时间内走完准确的路径.
//设置粒子移动.
for (int i = 0; i < num; i++)
{
//计算所用的生命时间与总周期时间的比例.
float proportion = (m_Particles[i].startLifetime - m_Particles[i].remainingLifetime) / m_Particles[i].startLifetime;
//根据比例,计算所在路径点的位置.继而设置方向.
int index = Mathf.FloorToInt(proportion * targetList.Count);
if (index >= 0 && index < targetList.Count - 1)
{
//设置粒子移动的方向.
Vector3 direction = targetList[index + 1] - targetList[index]; //模拟空间为World.
//Vector3 direction = m_Transform.InverseTransformPoint(targetList[index + 1]) - m_Transform.InverseTransformPoint(targetList[index]); //模拟空间为Local.
//设置移动的速度.
//m_Particles[i].velocity = direction * (1.0f / speed) * (1.0f / m_Transform.localScale.x);
m_Particles[i].velocity = direction * (1.0f / speed);
}
else
{
//超出路径之后,粒子销毁.
m_Particles[i].remainingLifetime = 0;
}
}
//重新赋值粒子.
m_ParticleSystem.SetParticles(m_Particles, num);
}
参考链接:
粒子移动
粒子移动
粒子移动