Search Unity

Repositioning TrailRenderer

Discussion in 'Scripting' started by BigBadPanda, Oct 23, 2017.

  1. BigBadPanda

    BigBadPanda

    Joined:
    Nov 28, 2014
    Posts:
    11
    Hello everyone,

    I am using a trail renderer in my game and sometimes I need to reset the position of my object.
    Is there a way to update positions of trail, so i can preserve the trail after I change the position of object.
    PS. I don't want to clear the renderer.

    Thanks.


    ProblemVisual.jpg
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    This might work:

    1 do a .SetActive(false) on the root game object (assuming the trail renderer is somewhere in that hierarchy)
    2 move the object to a new location
    3 do a .SetActive(true) on it

    If that doesn't work, then maybe do instead of the above:

    1 - same as above
    2 move the object to the tail of its trail renderer
    3 do a .SetActive(true) on it
    4 move it forward to its final location
     
    TheDevDude1 likes this.
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Thats what I was thinking. The waves are a child of the boat object. So you need to move the waves so that they are not a child anymore. then destroy the boat. or even better, copy the wave to a new object, then move the boat and original wave to a new location.
     
  4. BigBadPanda

    BigBadPanda

    Joined:
    Nov 28, 2014
    Posts:
    11
    Thank you for your input, but these didn't work:

    Code (CSharp):
    1. gameObject.SetActive(false);
    2. transform.position = new Vector3(transform.position.x, 0, 0);
    3. gameObject.SetActive(true);
    Code (CSharp):
    1. gameObject.SetActive(false);
    2. transform.position = new Vector3(transform.position.x, 0, -5);
    3. transform.position = new Vector3(transform.position.x, 0, 0);
    4. gameObject.SetActive(true);
    The problem here is probably, as it stated in documents, trail renderer needs to "laid out over a sequence of frames". But there should be an option to just assign vertices by code to move the trail.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Ah well, sorry to lead you astray. You might want to do your own trail renderer using the LineRenderer component and driving your own historical vertex positions.

    That way when you reposition the object, you can instantly reposition all the verts in your line renderer and have complete control.

    If you're unfamiliar with the LineRenderer, it also has a boolean that specifies if it uses world coordinates. You might be able to say "uses local coordinates" and then the act of moving it will also move the rendered output automagically. Obviously if you use it in this mode, you would need to feed in local coordinates (relative to the transform it is on) to get the right effect.
     
  6. vdKa

    vdKa

    Joined:
    Jan 22, 2014
    Posts:
    13
    trailRenderer.Clear();
     
    adamsaidane252 likes this.
  7. sdoddler

    sdoddler

    Joined:
    Nov 20, 2017
    Posts:
    2
    Thanks @vdKa This helped me so much.
    For anyone interested i used this solution:
    Code (CSharp):
    1.  tf.gameObject.SetActive(false);
    2.         TrailRenderer[] trails = tf.gameObject.GetComponentsInChildren<TrailRenderer>();
    3.         foreach (TrailRenderer t in trails)
    4.         {
    5.             t.Clear();
    6.         }
    7.         tf.position = CurCheckpoint.checkpointSpawn.transform.position;
    8.         tf.gameObject.SetActive(true);
     
  8. EmmaPrats

    EmmaPrats

    Joined:
    Apr 19, 2019
    Posts:
    8
    I tried that snippet, but it clears the trail when teleporting the ship, so it didn't solve my problem: I want to instantly teleport my ship with its trail, so that the player doesn't even know that happened. Luckily, I found out that you can edit the trail vertices, so this works perfectly for me:
    Code (CSharp):
    1. var newPosition = new Vector3(50, 10, 31); //arbitrary
    2. var offset = newPosition - transform.position;
    3. var trailRenderers = GetComponentsInChildren<TrailRenderer>();
    4. foreach (var trailRenderer in trailRenderers)
    5. {
    6.     var positionCount = trailRenderer.positionCount;
    7.     for (var i=0; i<positionCount; i++)
    8.         trailRenderer.SetPosition(i, trailRenderer.GetPosition(i) + offset);
    9. }
    10. transform.position = newPosition;