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.
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;
}
GetComponent<TrailRenderer>().Clear()
Handles this as of Unity-5-3
Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.
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
Just use trailRenderer.Clear()
: