using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TestParticleMovement : MonoBehaviour
{
public List<Vector3> nodes;
public Vector3[] directions;
private ParticleSystem particles;
void Start()
{
particles = GetComponent<ParticleSystem>();
var main = particles.main;
main.startLifetimeMultiplier = nodes.Count;
if (nodes.Count == 0)
Debug.LogError("请添加至少1个node");
directions = new Vector3[nodes.Count];
for (int i = 0; i < nodes.Count; i++)
directions[i] = (nodes[i] - ((i - 1 >= 0) ? nodes[i - 1] : transform.position));
}
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;
float dist = GetAddedMagnitude((int)timeALive);
int count = 0;
while (dist > GetAddedMagnitude(count))
{
count++;
particleList[i].velocity = directions[count];
}
}
particles.SetParticles(particleList, partCount);
}
private float GetAddedMagnitude(int count)
{
float addedMagnitude = 0;
for (int i = 0; i < count; i++)
{
addedMagnitude += directions[i].magnitude;
}
return addedMagnitude;
}
}
具体方向还是要大家自己测试
|