不说废话直接上代码,使用方法就是添加到粒子系统的Object下的脚本,然后在nodes中添加位置就可以了!
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
public class TestParticleMovement : MonoBehaviour
{
public List<Vector3> nodes;
private Vector3[] directions;
private ParticleSystem particles;
void Awake()
{
// 确保第一个位置是粒子发射起始位置
if (nodes[0] != transform.position)
{
nodes.Insert(0, transform.position);
}
// 设置生存时间为经过的点数
var mian = GetComponent<ParticleSystem>().main;
mian.startLifetime = nodes.Count - 1;
//自动生成方向,代表粒子速度,使粒子在一个生命周期内到达每个指定node
directions = new Vector3[nodes.Count-1];
for (int i = 0; i < nodes.Count-1; i++)
directions[i] = nodes[i+1] - nodes[i];
}
void Start()
{
}
void Update()
{
// 获取所有粒子
particles = GetComponent<ParticleSystem>();
ParticleSystem.Particle[] particleList = new ParticleSystem.Particle[particles.particleCount];
int partCount = particles.GetParticles(particleList);
for (int i = 0; i < partCount; i++)
{
// 计算粒子当前的生命
float timeALive = particleList[i].startLifetime - particleList[i].remainingLifetime;
// 设置粒子的方向速度为下一个node
if((int)timeALive < directions.Count())
particleList[i].velocity = directions[(int)timeALive];
}
particles.SetParticles(particleList, partCount);
}
}
确保粒子模拟控件是“世界”,不然可能出现坐标系错误