Particle Trail Renderer - How can I clear particles?

I have a trail renderer that is a modified version of the Trails - Trails Inherit Velocity trail from the Particles sample. I dont actually want to use velocity, and instead want to just have a simple trail follow a ball, but this was the only way I could get it to work.

My problem is that I stop the particle system when the ball hits a collider. However the trail continues to be rendered at what I assume is the velocity it has from the most recent frame. How can I clear this so it just stops. Deactivating the object with the particle is not an option.

I would prefer to just be able to have a standard trail renderer that doesnt use velocity, but Ive not had success with other options. Simply being able to stop the rendering of the trail when I want, would be acceptable.

Any help is appreciated.

Link to video of issue

Hi Nostalgicbear,

To stop the rendering of a particle trail when the ball hits a collider, you can utilize the stop() method of the particle system to halt the emission of new particles and reset their state. Based on the provided information, you can also clear existing particles when you want to ensure the trail stops. Here’s a code snippet that demonstrates this:

import { ParticleSystem } from "@needle-tools/engine";

// Assuming you have a reference to your particle system
const particleSystem = this.gameObject.getComponent(ParticleSystem);

// Method to stop and clear the trail
function stopTrail() {
    particleSystem.stop(false, true); // Stops the particle system and clears emitted particles
}

// Call this method when your ball hits a collider
function onBallHitCollider() {
    stopTrail();
}

In this code:

  • The stop() method is called with clear set to true, which removes all emitted particles and resets the particle system state.
  • This way, once the ball hits the collider, the trail will end immediately without continuing to render based on the last frame’s velocity.

If you haven’t already implemented your event detection for the ball hitting a collider, make sure to hook the onBallHitCollider() function into your collision detection logic.

For further customization or if you need a different behavior, consider creating a custom particle behavior that manipulates the trail length or other properties directly.

Feel free to let me know if you need additional information or examples!

3 replies left
thread_GERbpqWp9qn6neSCvoIsB8NP
Reply to this message to continue the conversation