Teleporting trail renderer
I have a trail renderer on a game object, I am teleporting said game object, when I do this the trail briefly forms between before and after the object was teleported. I would like to prevent this. How may I do this?
This is a 2D project, and I am using c#.
If you have any questions, please do not hesitate to ask, any help is much appreciated.
Thank you
Answer by robertbu · Sep 23, 2014 at 03:54 AM
Setting the trail renderer's time to 0, waiting a frame, then restoring the time works:
function ResetTrailRenderer(tr : TrailRenderer) {
var trailTime = tr.time;
tr.time = 0;
yield;
tr.time = trailTime;
}
An untested C# translation (would be called with StartCoroutine()):
IEnumerator ResetTrailRenderer(TrailRenderer : tr) {
float trailTime = tr.time;
tr.time = 0;
yield return null;
tr.time = trailTime;
}
Thank you, that worked perfectly. Seems slightly hacky to have to do it like this... It is the kind of thing that you would expect unity to have in their API's...
Problem with this method is, it doesn't actually restore the trail's length, it just starts making a new one from the new location. So it still doesn't actually teleport the trail.
No, I don't have a solution that does that (I've been looking for one...), just thought I'd point that out.
Answer by Binarin · Sep 07, 2016 at 03:23 PM
Trail Renderer now has a clear function, it may have been added to Unity after this question was asked. After you have moved the object call Clear and you shouldn't see the line.
Alternatively; if you don't want the trail to suddenly disappear at the position the object teleported from you can create a copy of the object where you want to teleport it to and enable auto destruct on the original.
Answer by Cherno · Sep 24, 2014 at 01:25 AM
Why not just disable the TR just before changing the transform.position, and enabling it again afterwards?
Does not work. It seems to pick up where it left off when you reenable.