- Home /
Remove all particles from TrailRenderer
I'm implementing a sword mechanic and I'm using a trailrenderer to show the swish which is enabled (by setting the time to a value) when the left-mouse is down and disabled when mouse up.
However, if the player makes the next swing quickly, the trail jumps to the start position and creates a connected line.
I would like to clear all the particles for the next swing but TrailRenderer.ParticleEmitter.ClearParticles() doesn't seem to work.
How can I immediately clear all the particles for the next swing?
Answer by Aubrey Hesselgren · Aug 16, 2011 at 03:05 PM
I'd love to know for something similar: I have an object which is on a wrap-around map, and crossing the "seam" (repositioning the player by one world-unit) looks glitchy, leaving behind a big streak because I don't know how to access the trail points directly.
Answer by testure · Aug 16, 2011 at 03:41 PM
you can set the length of the trail to zero, and then restore it when you want it to reset, but this is the only way to do it short of destroying the trail renderer and recreating it each time.
Answer by sampenguin · May 10, 2012 at 07:33 PM
I just solved this similar problem by creating instances of the trail renderer from a referenced prefab, and then destroying the instance at the points in time when I need an absolute clean visual break in the trail (and of course, creating a new instance for the next trail). It's a bit of code overhead but the only way I could come up with something that worked after a day of trying everything else.
In your sword example, I would suggest you allow for something like an array/list of swings, where each swing Instantiate()s its own new trail that can fade to completion correctly, and then destroy itself after all fx complete.
Answer by Wabo · Feb 20, 2013 at 09:14 PM
What worked for me is this:
//Declare your temporary timer.
private float _trailTimer = 0.1f;
void YourFunction()
{
this.gameObject.GetComponentInChildren<TrailRenderer>().time = 0f; //Disable the Trail Renderer.
}
void Update()
{
//Reset the trail of the TrailRenderer so that it wont glitch through the
//screen while pooling the object.
if(this.gameObject.GetComponentInChildren<TrailRenderer>().time == 0)
{
_trailTimer -= 1 * Time.deltaTime;
if(_trailTimer <= 0)
{
this.gameObject.GetComponentInChildren<TrailRenderer>().time = 1.5f;
_trailTimer = 0.1f;
}
}
}
Answer by atreyuroc · Jul 04, 2018 at 09:05 PM
I know this is an old post, but I hope this solution can help someone.
The work around I found for this issue was to set the TrailRenderer.time to negative one. This will ensure that any existing trails are removed. But you can not reset the trail time back to the default value at the same time. For some reason you have to let unity process the -1 for a frame or two.
Here is some example code.
public class Ball : MonoBehaviour {
private TrailRenderer tr;
// Use this to hold what ever the current time might be.
private float trailTime;
void Start(){
tr = GetComponent<TrailRenderer>();
}
private void ResetTrailTime()
{
tr.time = trailTime;
}
public void ResetTrail()
{
// Save the old time
trailTime = tr.time;
// Set to -1
tr.time = -1;
// Wait some frames and then set the trail time back.
Invoke("ResetTrailTime", 0.001f);
}
}