- Home /
how to control particles along a path?
Greetings unity community, i am trying to script a particle path to represent the flow of electricity through a wire with different pathways that can be placed at random. in essence its just a path follow script. currently i have 3 points that represent connection and a way to chose what point the particle should go to next. the main problem i am having is that after i stop the particle flow errors occur in the pathways and particles go all over the screen. i tried to solve this by "binding" each particle to the point it needs to go to next but this hasn't helped at all. with the code provided can someone help me to find either a more efficient method to accomplish this or find were the errors are occurring?
using UnityEngine; using System.Collections;
public class ParticleControl : MonoBehaviour { //different veriables that are used in the simulation public float Offset; public float Speed; public float Emission; float NextTime; public GameObject[] Node; int Alive; bool StartSim = false;
ParticleSystem Parts;
ParticleSystem.Particle[] PList;
int[] List;
// Use this for initialization
void Start ()
{
Parts = GetComponent<ParticleSystem> ();
PList = new ParticleSystem.Particle[Parts.maxParticles];
List = new int[PList.Length];
}
// Update is called once per frame
void Update ()
{
// controlling the emission rate of the particle system
if (NextTime <= Time.time && StartSim)
{
Parts.Emit (1);
NextTime += Emission;
}
// starting and stoping
if(Input.GetKeyDown(KeyCode.Space))
StartSim = !StartSim;
// getting the number of particles that are active
Alive = Parts.GetParticles(PList);
for (int R = 0; R < Alive; R++)
{
// if the particle just started set its path point to 0: the first point in the node list
if (PList [R].startLifetime - PList[R].lifetime < 0.1)
List [R] = 0;
// tell the particle to head to its bounded node point List[R] keeps track of the points, Node[] holds all possible nodes
PList [R].velocity = Vector3.Normalize(Node [List[R]].transform.position - PList[R].position) * Speed;
// if the particle has reached its bounded node point deturmin the next node
if (PList [R].position.x >= (Node [List [R]].transform.position.x - Offset) && PList [R].position.x <= (Node [List [R]].transform.position.x + Offset))
{
if (List [R] == 0)
List [R] = Random.Range (1,3);
else if (List [R] == 1)
List [R] = 2;
else if (List [R] == 2)
List [R] = 3;
// if the node reached is the last node terminate the particle that reached it
else if (List [R] == 3)
{
PList [R].lifetime = 0;
List [R] = 0;
}
}
}
// set the paramters for the particles
Parts.SetParticles (PList, Alive);
}
}
Your answer
Follow this Question
Related Questions
How to store gameobjects with specific scripts in a list ? 1 Answer
Multiple Cars not working 1 Answer
Query with C# arrays and List<> 2 Answers
How to modify array values? 1 Answer
A node in a childnode? 1 Answer