Is there anyway to reset/clear Trail Renderer?
Asked Answered
T

4

0

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 every time 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.

Tania answered 17/3, 2024 at 23:15 Comment(0)
R
0

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

Raisin answered 17/3, 2024 at 23:17 Comment(2)

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.

Mnemosyne

Work for me! Thanks

Ethiopic
W
0

New Unity version has a Clear method for trails:

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;
    }
}

float _ClearTimeS = float.NaN;

public void Clear()
{
    if (!float.IsNaN(_ClearTimeS)) return;
    _ClearTimeS = _TrailRenderer.time;
    _TrailRenderer.time = 0;
}
Wilkerson answered 17/3, 2024 at 23:16 Comment(0)
L
0
GetComponent<TrailRenderer>().Clear()

Handles this as of Unity-5-3

Leprosy answered 17/3, 2024 at 23:17 Comment(0)
B
0

Just use trailRenderer.Clear():

Bakeman answered 17/3, 2024 at 23:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.