Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MyLittlePony69 · May 19, 2014 at 08:54 AM · c#particlesvelocityparticlesystem

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MyLittlePony69 · May 20, 2014 at 05:27 AM 0
Share

Thanks!!!!

avatar image Chris_Dlala · May 20, 2014 at 08:06 AM 0
Share

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 =)

avatar image joehot200 · Dec 07, 2016 at 11:03 AM 0
Share

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?

avatar image
5

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;
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nadhimali · Jul 01, 2016 at 10:50 AM 0
Share

It didn't work for me

avatar image dpakmenon · Oct 31, 2016 at 07:33 AM 0
Share

just what i needed . cheers! works!

avatar image TasukuTakahashi · Jun 27, 2018 at 04:12 PM 0
Share

It worked for me too. Thank you very much!

avatar image
2

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;
     }
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image larswik · Oct 21, 2017 at 05:56 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges