- Home /
Can you change the Velocity Over Lifetime of a Particle System using a Script?
Can you change the Velocity Over Lifetime of a Particle System using a Script?
If so, then how?
If not, then is there a way of changing a velocity of the Particle System when I press a button? The object that the Particle System is attached to is not moving and always stays at 0,0,0.
This is my current script that makes the Particle stop when I press the Up button. But instead of stopping, I want it to change its velocity, or make it swing.
void Update () {
if(Input.GetKeyDown(KeyCode.UpArrow))
{
particleSystem.Stop();
}
}
The documentation of Unity about the Particle System doesn't have any code for changing its Velocity Over Lifetime.
So does it not exist or is it only not written there?
Thanks!
Answer by Chris_Dlala · May 19, 2014 at 01:21 PM
You can get access to an array of particles in a current system using:
ParticleSystem.Particle[] particles = new ParticleSystem.Particle[particleSystem.particleCount];
int count = particleSystem.GetParticles(particles);
Then you can iterate over the array and set the particles after. The reference page for particle can be found here.
for(int i = 0; i < count; i++)
{
float yVel = (particles[i].lifetime / particles[i].startLifetime) * distance;
particles[i].velocity = new Vector3(0, yVel, 0);
}
particleSystem.SetParticles(particles, count);
Place it all in a late update and that should work.
float distance = 3;
void LateUpdate ()
{
ParticleSystem.Particle[] particles = new ParticleSystem.Particle[particleSystem.particleCount];
int count = particleSystem.GetParticles(particles);
for(int i = 0; i < count; i++)
{
float yVel = (particles[i].lifetime / particles[i].startLifetime) * distance;
particles[i].velocity = new Vector3(0, yVel, 0);
}
particleSystem.SetParticles(particles, count);
}
This is my first post so apologies for any formatting mistakes.
No problem. I'm glad I could help. I don't want to be "that guy" but can you accept my answer if it's what you needed =)
So there's no way to just change the initial velocity of the emitter? I have to keep spam-iterating through them to check for newly spawned particles?
Answer by joni.giuro · Mar 18, 2016 at 12:52 PM
I'm on unity 5.3 and it is much more straightforward:
// Get the Velocity over lifetime modult
ParticleSystem.VelocityOverLifetimeModule snowVelocity = GameObject.Find ("Snow").GetComponent<ParticleSystem> ().velocityOverLifetime;
//And to modify the value
ParticleSystem.MinMaxCurve rate = new ParticleSystem.MinMaxCurve();
rate.constantMax = 10.0f; // or whatever value you want
snowVelocity.x = rate;
Answer by Mehrdad995 · Oct 11, 2017 at 03:33 PM
it's even more straight forward in Unity 2017.1.1f1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExampleClass : MonoBehaviour
{
[SerializeField]
private float speed;
[SerializeField]
private ParticleSystem particle;
private ParticleSystem.VelocityOverLifetimeModule velocityModule;
void Start()
{
velocityModule = particle.velocityOverLifetime;
}
void Update()
{
velocityModule.zMultiplier = speed;
}
}
Thank you! I looked a couple months ago before this was answered and didn't find the right answer. I used it to change the direction and speed of my snow in a 2D game using the ForceoverLifetime$$anonymous$$odual.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class snowFallingScript : $$anonymous$$onoBehaviour {
public ParticleSystem particle;
ParticleSystem.ForceOverLifetime$$anonymous$$odule force$$anonymous$$od;
public void checkAndAdjustSnowDirection(){
force$$anonymous$$od = particle.forceOverLifetime;
float value = (GameObject.Find("GameScript").GetComponent<GameScript>().windValue == 0)? 1 : GameObject.Find("GameScript").GetComponent<GameScript>().windValue / 10f;
force$$anonymous$$od.x$$anonymous$$ultiplier = value;
}
}
Your answer
Follow this Question
Related Questions
How to make an effect in a particle system so that it is always behind the sphere? 1 Answer
How to get newest Particle in a ParticleSystem 1 Answer
Particles dissapearing when camera is close 1 Answer
Distribute terrain in zones 3 Answers
How does particle.velocity relate to the particle's actual velocity? 2 Answers