- Home /
C# Changing Force over Lifetime Through Script
I would like to change certain axes in the force over lifetime feature in a shuriken particle system through a script. But I'm not sure where it is. I've searched on the Unity script reference and I couldn't find anything along the lines of force over lifetime. I'm not even if it is possible to access it through this manner. I've tried ParticleSystem.ForceOverLifetime.Vector3.X but ForceOverLifetime is not in the class ParticleSystem.
There might not be a direct Unity API to do that yet, but this UA post talks about how to do it manually - the example uses color over lifetime but you might be able to adapt it to force over lifetime
http://answers.unity3d.com/questions/347675/how-to-change-particle-systems-color-over-lifetime.html
I looked and there is no vector3 variable called force so I don't think that will work.
There's no "force", but there is velocity. Force causes change in velocity.
Answer by Scopperloit · May 13, 2014 at 01:13 AM
If you want to change the behaviour of each particle individually, you will have to create a storage space in terms of an array, and use particleSystem.GetParticles(yourArray);
to store the entire particle system. After altering each particle, you need to put the array back into the particle system like this: particleSystem.SetParticles(yourArray, int size);
Next you need to loop through the array using a for-loop, then change the velocity of each particle.
I've used this technique to make particles orbit a sphere by simulating a gravitational pull on each individual particle. Performance-wise it works fine on my desktop with at least 10.000. I'm not sure how this many particles performs on a phone, though. If you want to see how it all works out, take a look at my YouTube-video.
Here's a suggestion for how to set up the code:
private void AddForceOnParticles(){
ParticleSystem.Particle[] _particles = new ParticleSystem.Particle[particleSystem.particleCount];
this.particleSystem.GetParticles(_particles);
for (int i = 0; i < _particles.Length; i++){
// Here you can do whatever you want with each particle by using _particles[i]
}
this.particleSystem.SetParticles(_particles, int size);
}
Call the AddForceOnParticles()
from the Update()
method.
Hope this helps somehow! :)
Your answer
Follow this Question
Related Questions
C# Emit Particle Upon Left Mouse Click 2 Answers
Player lives script help 1 Answer
C# -- Build character unit from script 1 Answer
C# Randomly Adding Elements from stringListA to stringListB 1 Answer
C# Input.GetKey("Tab") Double Tap 1 Answer