- Home /
How do I set the position of a newly emitted particle in a local enabled particleSystem?
Any help would be Greatly appreciated; I have been having this problem for over a week.
here is my current code attempt, made from what I could find in other unity answers and forums, problem is all of the particles apear at the same position, even when the position smoothly changes, all of the particles move as if attached to the hit point instead of leaving a trail behind.
function EmitAtPos(position: Vector3, amount: int){
particleSystem.Emit(amount);
var particles: ParticleSystem.Particle[] = new
ParticleSystem.Particle[amount];
if (particleSystem.GetParticles(particles) < amount) {
Debug.Log("Hey!");
return;
}
for (var i: int = 0; i < amount; i++) {
particles[i].position = position;
}
particleSystem.SetParticles(particles, particleSystem.particleCount);
Debug.Log(position);
}
Answer by CodeMasterMike · Dec 20, 2012 at 03:24 PM
You need to call the ParticleSystem.SetParticles-function as well, otherwise the changes will not be stored. Like so (in C# though, but the technique is the same):
ParticleSystem m_currentParticleEffect = (ParticleSystem)GetComponent("ParticleSystem");
ParticleSystem.Particle []ParticleList = new ParticleSystem.Particle[m_currentParticleEffect.particleCount];
m_currentParticleEffect.GetParticles(ParticleList);
for(int i = 0; i < ParticleList.Length; ++i)
{
ParticleList[i].position = newPosition;
}
m_currentParticleEffect.SetParticles(ParticleList, m_currentParticleEffect.particleCount);
Link to SetParticles documentation.
Good luck!
Ok, i added the setparticles part, and made sure all the code worked properly. However, when i ran the code, all of the particles got added to the same position(I double checked that the position was changing with a debug.log, so the particles are definitly not being added to multiple positions. I updated the original code to reflect the new changes. Is it possible that the setparticles function is overwriting the positions of the previous particles?
I doubt that the SetParticle function would do something like that.
The issue what I can see, is that you have the position variable as a inparameter to the function. This means it will not change until next time you call the function. And therefor you set all particles to the same position.
And just so that you know, you are doing the Debug.Log(position); call at the very last in the function, meaning that the position only shown once, and it is not showing the individual position of the particles.
This code displays the position for each individual particle from the particle list:
for (var i: int = 0; i < amount; i++)
{
particles[i].position = position;
Debug.Log(particles[i].position);
}
Ok, so this part is working as it should, but what I am trying to do is not compatable with this method. I have a laser that calls this once per frame that the laser hits something moving. The laser itself is also moving, so when the laser hits the object, it adds the particle system to a new object that becomes parented to the object that was hit. Then for every frame the laser hits the object, it adds particles by calling this function and passing in the hit point. I am baisically trying to get the laser to draw particles on the object. So..is there any way of doing this with the available tools?
I don't have a answer to that question at the moment.
But you should put that question in a new separate thread, mostly because it is a new type of question, and you would get more answers that way.
And it would maybe help other people with the same question, since its easier to find when searching.