- Home /
Recycled trailers leave streaks...
How to reset the trail of a trail renderer so when I recycle and reactivate pooled objects they don't leave streaks?
I've tried turning off the TrailRenderer and ClearParitcles on the particle emmiter when they are reused but with no joy so far...
Example Bullets, 1 Created/Recycled sets position and rotation (reactivated) 2 Fired adds velocity 3 Stop/Explode 4 Deactivated - Velocity Zeroed 5 Pooled 6 Reused back to 1
Am I missing something, in effect I want to restart/reset the trail when I reactivate the object?
Answer by turbanov · Feb 11, 2016 at 01:05 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 efge · Mar 01, 2011 at 01:38 PM
You should make a prefab for your bullet and add a TrailRenderer, use Instantiate to use your bullet and Destroy after collision or explosion. This activates and kills your TrailRenderer as desired.
See Also: In depth Prefab Instantiate discussion
Cheers I was trying that without the prefab and it was getting messy having to set things by hand.
Using a prefab would work much better, but found a work around disable the trail then when the bullet is fired Invoke a function that will re-enable it with a delay from it's time property. It works but not sure it it would be better performance wise just to delete and recreate.
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 maesty · Oct 14, 2013 at 11:53 PM
function FixTrail() {
var tr : TrailRenderer = GetComponent(TrailRenderer);
var tmp : float = tr.time;
tr.time = -1;
yield;
tr.time = tmp;
}
Your answer
Follow this Question
Related Questions
Trail renderer leaves streaks 0 Answers
How to disable script in MainCamera game object 1 Answer
image not enabling 0 Answers
How to disable an certain animation from my player? 0 Answers
Best animation plugin that works with the new 2D stuff? 0 Answers