Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Putting a TrailRenderer to "rest"

Discussion in 'Editor & General Support' started by manuelflara, Feb 25, 2007.

  1. manuelflara

    manuelflara

    Joined:
    Jan 21, 2007
    Posts:
    87
    I have an object that has a trailrenderer, so it leaves a trail when moving. The thing is, when this object gets outside a given boundary, I respawn it on someplace else. And when I do that, for a moment, the trail is drawn using the positions previous to the "teleportation". One way of solving this would be removing and adding the component at respawn, but is there any other way of fixing this? Weirdly, I've tried to make an screenshot showing the effect, but the output image does not show the trail.. ??
     
  2. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    It sounds like the only way to fix this is to use a child object with nothing but the trail renderer, make sure autodistruct is on and then do this:

    Code (csharp):
    1.  
    2. //right before I am teleported
    3.  
    4. trail.transform.parent = null;
    5.  
    6. //teleport, then
    7.  
    8. trail = Instantiate(trailPrefab, transform.position, transform.rotation);
    9. trail.transform.parent = transform;
    10.  
     
  3. sidestepper

    sidestepper

    Joined:
    Oct 2, 2008
    Posts:
    96
    I know this is a crazy old thread, but I wanted more to it.

    I too want to "teleport" something with 2 trail renderers on it, but my first instinct(Rather than unparent or destroy then reinstantiate) was to calculate the distance of the teleport, then set the min vertex distance on the trail renderers to that number(Probably a little greater, or better yet Mathf.Infinity) to ensure that the distance I am moving my object would technically not be great enough for the trail renderer to spawn a particle, then promptly after the teleport set the distance back to 0.1 or whatever smaller increment is was prior.

    Problem is, it seems that property is not exposed in script???? I tried everything, min_vertex_distance, MinVertexDistance, etc etc, and the script reference(http://unity3d.com/support/documentation/ScriptReference/TrailRenderer.html) does not show it as a variable, property, function nothing, yet you can set this in the inspector at design time - how do we change it at run time?
     
  4. Charles-Van-Norman

    Charles-Van-Norman

    Joined:
    Aug 10, 2010
    Posts:
    86
    Here's a workaround that worked for me

    Code (csharp):
    1.  
    2.     void Update () {
    3.         transform.position += Vector3.up*Time.deltaTime; //move up every frame
    4.         if ( Vector3.Magnitude(transform.position-startPos) > maxMoveDist ) { // moved too far, teleport
    5.             GetComponent<TrailRenderer>().time=0; // set time to 0 which "destroys" trail renderer and it won't draw from point a to b this frame
    6.             transform.position = startPos; // teleport the gameobject
    7.             StartCoroutine(ResetTrailDist()); // must reset time to 1 in a coroutine or it will reset it this frame, resulting in no change
    8.         }
    9.     }
    10.  
    11.     IEnumerator ResetTrailDist(){
    12.         yield return new WaitForSeconds(.1f);
    13.         GetComponent<TrailRenderer>().time=1;
    14.  
    15.     }
    16.  
     
  5. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    Or just:

    Code (CSharp):
    1. IEnumerator ResetTrailDist(){
    2.        yield return null; //wait a single frame
    3.        GetComponent<TrailRenderer>().time=1;
    4. }
     
  6. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,366
    You know, this thread has been dormant for almost a decade. If you're going to necro-post, add something more useful and interesting than
    yield return null
    , please.