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

Bug Trail renderer creates a trail on reactivation!

Discussion in 'General Graphics' started by FeastSC2, May 19, 2020.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    A Trail Renderer draws a line between its last active position and its current position on re-activation.

    This bug only occurs if the Trail would still be drawn had you not turned off the Trail (set a long lifetime on the trail to make the bug occur).

    To reproduce the bug:
    - create a trail renderer and set its time to 20
    - move the trail some, then disable the trail renderer component or its gameobject
    - move the trail some more, then re-enable the component or gameobject
    - observe that the line suddenly reappears, additionally a line is now drawn between the point where you disabled the trail and re-enabled it.

    EHT1nxcMFe.gif

    This bug can be avoided by calling: TrailRenderer.Clear() in OnDisable.

    This has to be a bug, right?
     
    vozcn likes this.
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    What would you expect to happen? For the trail points to be removed when it is disabled?
    I guess what is happening, is that the trail enters a kind of "paused" state while it is disabled.

    I doubt we will want to change this, given that you can easily use Clear .. there is a risk of breaking other users' content.
     
    karl_jones likes this.
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Yes the points should be removed when it's disabled, this is what happens in a particle system with trails gets disabled for example.

    It's not exactly behaving like a pause because if you set a short Lifetime: say 3s, make it draw then quicky disable it:
    - If you reactivate the trail after more than 3 seconds, you won't have any points drawn.
    - If you reactivate the trail before 3 seconds have passed, then you will have points drawn.

    I saw this bug when using a pooling system on fx with trails.

    Yes, I expected this, not a problem :)

    It's as simple as adding this script to fix the issue anyway:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(TrailRenderer))]
    4. public class TrailRenderer_ClearOnDisable : MonoBehaviour
    5. {
    6.     void OnDisable()
    7.     {
    8.         var trail = GetComponent<TrailRenderer>();
    9.         trail.Clear();
    10.     }
    11. }
    To be clear: I don't need anything changed, the question below is just me being curious.

    Is there really no way to make new default behaviours for future versions without breaking user content?
     
    vozcn, huulong, mvaz and 1 other person like this.
  4. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,281
    There are ways to change behaviour for new content, but it can get weird and messy (imagine having a mix of upgraded vs new assets..)

    If particles get cleared out when disabling the component, Im more inclined to agree with you about it being a bug.. still scared to change it tho ;)

    I guess when it’s unpaused any points that would have died due to too much time passing will be all removed instantly when enabled. That seems like a more clear cut bug. If disabling doesn’t clear out the points then time shouldn’t pass for the trail while disabled either.
     
    FeastSC2 likes this.
  5. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    does trail.clear() actually work?

    It does absolutely nothing for me. Do you need to do this in a specific way?
     
  6. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    never mind... figured it out. I was setting a position as soon as cleared it. I had to set the position first, then clear it.
     
    jctn_nemo and richardkettlewell like this.
  7. Torbach78

    Torbach78

    Joined:
    Aug 10, 2013
    Posts:
    296
    including a 'clearOn' operation in the component with a 'onDisable' or 'onActivation' could allow users options; without causing existing content to have it enabled.

    typically in pooling logic I have had to ask engineers to instantiate + move, then next update tic to activate... otherwise behavior can cause long streaks across game content, including the inherintVelocity on shuriken making particles wiz across the screen on the frame they awaken
     
    huulong and richardkettlewell like this.
  8. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    this was my case too, it was coming from a pool.
     
  9. unitj75

    unitj75

    Joined:
    May 30, 2022
    Posts:
    26
    I'm seeing a similar issue but slightly different. When moving the PS, some of the particles jump to the new location, then back and continue their remaining journey, creating a streak. It only happens in world simulation mode, and it doesn't matter how the trail is set (World checked or unchecked).