With a simple particle system how can I control it’s particle emission volume so that it emit’s more and more the faster my character goes, through script?
It would need to detect my characters velocity constantly and emit only when the character is in motion, but how do I write it in C#?
You can tag a particle system to emit based on distance rather than time. Would that solve your problem? The faster you go, the more distance you cover, the more particles you’ll see. See the Emission subheader in the ParticleSystem inspector.
Hey Kurt. I think it’s a plan B but it wouldn’t be optimal because I’ve set it up so that my character has 2 speeds and they both accelerate over time to a certain top speed. So If I hold down shift and W to move fast for a short burst forward, the distance will not be great but the speed will and there will be a discrepancy with the number of particles emitted.
There’s got to be a way to set the number of particles emitted to be relative to speed, I just can’t find it anywhere.
Love your enthusiasm, it’s inspiring given that I still haven’t made it work perfectly but I’m getting closer.
You’re absolutely right in that I need .rateOverTime. Here’s a boiled down version of my script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public ParticleSystem myParticleSystem;
public ParticleSystem.EmissionModule emissionModule;
void Awake()
{
myParticleSystem = GetComponent<ParticleSystem>();
emissionModule = myParticleSystem.emission;
}
void FixedUpdate
{
emissionModule.rateOverTime = speed;
}
I’ve got a speed float that I use for forward movement in the direction my mouse is looking that didn’t include here because my script is very long, but what happens now is that when speed is used the particles speed accelerates, it’s not relative to my characters speed.
I also get this error:
NullReferenceException: Do not create your own module instances, get them from a ParticleSystem instance
UnityEngine.ParticleSystem+EmissionModule.set_rateOverTime (MinMaxCurve value) (at C:/buildslave/unity/build/artifacts/generated/common/modules/ParticleSystem/ParticleSystemBindings.gen.cs:955)
Fly.FixedUpdate () (at Assets/assets/Scripts/Fly.cs:57)
I’ve looked up the error on google, tried to inclued :UnityEngine.ParticleSystem+EmissionModule.set_rateOverTime (MinMaxCurve value) in my code endless ways and I can’t get it to work.
Thanks. Unity3D is single-handedly responsible for rekindling the excitement in game development that I’ve had since about 7th grade.
Okay, I put a little scene and script together, see the attached unitypackage. Run it in the editor, then go to the inspector and move the Radius and RateOverTime sliders up and down to see the effect. There is one scene, one script… script copied below for easy viewing:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParticleController : MonoBehaviour
{
[Header( "Drive these two sliders to change\nshape and rate over time.")]
[Range( 1,5)]
public float Radius = 1;
[Range( 0, 500)]
public float RateOverTime;
ParticleSystem.EmissionModule em;
ParticleSystem.ShapeModule sm;
void Start ()
{
ParticleSystem PS = GetComponent<ParticleSystem>();
em = PS.emission;
sm = PS.shape;
}
void Update()
{
ParticleSystem.MinMaxCurve mmc = em.rateOverTime;
mmc.constant = RateOverTime;
em.rateOverTime = mmc.constant;
// you could change other properties in here too obviously
sm.radius = Radius;
}
}
That is awesome. consider me inspired! Your test scene is EXACTLY what I needed to move forward Thank you.
Out of curiosity, are you self taught in C# since you started in the 7th grade? if so you’ve come a long way. The later you start the harder it is to learn, I think. I’m34 and I’ve been doing it for 3-4 years as a hobby but I love it and I’m learning all the time
I optimized your script to fit my needs and now it looks like there’s wind which accelerates relative to the speed of my player. It’s beautiful! This is why I love this, right now I feel Great! Your help was what I needed to get it to work and it’s much appreciated my friend.
Here’s the code for reference:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParticleController1 : MonoBehaviour
{
public float speed = 0;
Vector3 lastPosition = Vector3.zero;
//[Header( "Drive these two sliders to change\nshape and rate over time.")]
[Range( 1,5)]
public float Radius = 1;
[Range( 0, 500)]
public float RateOverTime;
ParticleSystem.EmissionModule em;
ParticleSystem.ShapeModule sm;
void Start ()
{
ParticleSystem PS = GetComponent<ParticleSystem>();
em = PS.emission;
sm = PS.shape;
}
void Update()
{
sm.radius = Radius;
}
void FixedUpdate()
{
speed = (transform.position - lastPosition).magnitude;
lastPosition = transform.position;
RateOverTime = speed;
}
}
Thanks @Jeepster … glad to hear it was helpful to you. I’ve only used C# for about five or six years. I learned C in 1989 and it really upshifted my engineering career, as it had been assembly language and BASIC prior to that.
Mentally I still think in terms of C much more than object oriented C#/C++ stuff, but I make do more or less. Always something to learn and really there is no better place to learn than in Unity3D. To hear me rave on about Unity, you probably think I’m an employee of Unity but I’m not. I just buy and love their products!