im fairly new to Unity and I guess it’s a pretty easy task, but I was wondering how to start emitting particles from a particle system upon moving? I tried to use the Play() and Stop() functions, but as soon as I use the Stop() function anywhere in my code it just never starts playing.
This is the Code im trying to use to control it. horizontalMove is my actual movement speed and GroundCheck() is pretty self-explanatory. Any Ideas?
ParticleSystem.Stop() is a bit unintuitive in that it sort of “locks” the particle system and waits for all emitted particles to disappear before giving back control.
Assuming everything else is correct, the ParticleSystem.Stop() is likely the issue.
Instead, you can use ParticleSystem.emission to control when the particles are shown.
I do almost what @Unrighteouss does, except usually I vary the emission amount based on a quantity such as ‘speed’ or ‘power’, so that slow motion produces fewer particles than fast motion for instance.
Here’s the engine motor control for the particle system in Jetpack Kurt:
It’s actually a totally generic Datasack → ParticleSystem emit over time controller, but the relevant non-Datasack code would be:
at Start():
ParticleSystem ps = GetComponent<ParticleSystem> ();
em = ps.emission; // em is class-private
BaseEmitOverTimeRate = em.rateOverTime.constant;
And then when value changes (or every frame if you’re lazy, I doubt it matters):
// where percentage is the "input" on off value (0.0 to 1.0 nominally)
em.rateOverTime = percentage * BaseEmitOverTimeRate;
Sadly it still doesnt show the particles on my end. I figured it might be easier to spot any mistakes with the full code, so here it is (it’s my playerController, still WIP, trying things out):
public class PlayerController : MonoBehaviour
{
[SerializeField] private float speed = 0f;
[SerializeField] private float jumpPower = 0f;
// Particlesystem for trail when moving on ground
[SerializeField] private ParticleSystem leftTrail;
[SerializeField] private ParticleSystem rightTrail;
public LayerMask groundLayer;
public BoxCollider2D bc;
public Rigidbody2D rb;
private float horizontalMove;
// Update is called once per frame
void FixedUpdate()
{
horizontalMove = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(horizontalMove * speed, rb.velocity.y);
if (Input.GetAxis("Jump") > 0 && GroundCheck())
{
rb.velocity = new Vector2(rb.velocity.x, jumpPower);
}
var leftEmission = leftTrail.emission;
var rightEmission = leftTrail.emission;
if (rb.velocity.x > 0 && GroundCheck())
{
leftEmission.enabled = true;
}
else
{
leftEmission.enabled = false;
}
if (rb.velocity.x < 0 && GroundCheck())
{
rightEmission.enabled = true;
}
else
{
rightEmission.enabled = false;
}
}
// Checks if the PlayerObject is on the Ground. To avoid jumping on walls it creates a BoxCast with an x slightly smaller than the total size of the BoxCollider
private bool GroundCheck()
{
float extra = 0.1f;
// Creation of BoxCast
RaycastHit2D hit = Physics2D.BoxCast(bc.bounds.center, new Vector3(bc.bounds.size.x - 0.187f, bc.bounds.size.y, bc.bounds.size.z), 0f, Vector2.down, extra, groundLayer);
// Checks for collision here
if (hit.collider != null) return true;
return false;
}
}
Thanks guys, Im pretty sure you can see that I am absolutely new to Unity :3
I tested out your exact script, and the the left particles work (just in the wrong direction), so if they’re not working for you there’s likely something else that’s wrong. The one thing that comes to mind that might be the issue is that your ground layer isn’t set on your ground objects.
The issue that’s causing the right particle system to not work (and messing with the left) is on line 29: var rightEmission = leftTrail.emission; - I’ll let you figure out what’s wrong here
This wasn’t causing any problems for me, but you should also change your FixedUpdate() method to Update(). The former is meant for physics calculations, and setting velocity isn’t a calculation, so you can do that in Update().
One last thing is that on line 61 you have an “if” statement without any curly brackets. Apparently this works (which I actually didn’t know), but it looks a bit confusing and it’s recommend to always use curly brackets.
If it’s still not working, let me know, but after you make the above changes there won’t be anything wrong with the script.
Cheers man, I just figured it out. Dumb me disabled “Play on awake”. Im just stupid.
About the if-statement: I’ll leave it as it is. I learned that it is slightly faster in compiling as it does not expect a “block” of instructions, but rather just one instruction as it is a one-liner.
But cheers for the support guys, can be closed down