- Home /
Is there anyway to reset/clear Trail Renderer?
Is there anyway to reset the Trail Renderer or clear the trail of Trail Renderer?
I'm currently using object pooling to avoid performance issue about run-time instantiation, but the trail renderer doesn't reset correctly -- it leaves streaks everytime it gets enabled.
I had searched a lot of answers and threads, the closest one is to set time to negative on disable, and wait a few frame to set it back. But this approach still won't work if the bullet runs faster.
Answer by turbanov · Feb 11, 2016 at 01:06 PM
New Unity version has a Clear
method for trails: http://docs.unity3d.com/ScriptReference/TrailRenderer.Clear.html
If this option is not available for you I recommend resetting the time in the OnRenderObject
callback of your script:
void OnRenderObject()
{
if (!float.IsNaN(_ClearTimeS))
{
_TrailRenderer.time = _ClearTimeS;
_ClearTimeS = float.NaN;
}
}
private float _ClearTimeS = float.NaN;
public void Clear()
{
if (!float.IsNaN(_ClearTimeS)) return;
_ClearTimeS = _TrailRenderer.time;
_TrailRenderer.time = 0;
}
Answer by bitstrider · Aug 29, 2016 at 09:29 AM
GetComponent<TrailRenderer>().Clear()
handles this as of 5.3
https://docs.unity3d.com/530/Documentation/ScriptReference/TrailRenderer.Clear.html
Answer by tarahugger · Aug 24, 2014 at 09:36 PM
The following works for me to move a trailed object without it streaking.
Place this extension in a c# file somewhere in your assets folder.
public static class TrainRendererExtensions
{
/// <summary>
/// Reset the trail so it can be moved without streaking
/// </summary>
public static void Reset(this TrailRenderer trail, MonoBehaviour instance)
{
instance.StartCoroutine(ResetTrail(trail));
}
/// <summary>
/// Coroutine to reset a trail renderer trail
/// </summary>
/// <param name="trail"></param>
/// <returns></returns>
static IEnumerator ResetTrail(TrailRenderer trail)
{
var trailTime = trail.time;
trail.time = 0;
yield return 0;
trail.time = trailTime;
}
}
then use it from within a monobehavior like so
trail.Reset(this);
see also
I had to change the yield at line 20 for this one to make it work:
yield return new WaitForEndOfFrame();
This will work as long as you have a camera rendering your scene.