Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trail renderer emit switch off

Discussion in 'Scripting' started by Happystudio, Mar 14, 2019.

  1. Happystudio

    Happystudio

    Joined:
    Mar 1, 2015
    Posts:
    4
    I have a trail renderer on a 3d object, when i use script to turn emit off and then move the object, the trail will still continue for that movement.

    I am using this code to switch it off:

    trail.emitting = false;
    transform.position = pos;

    e.g. starting at point A i switch off the emitter, move to point B and then on to point C. the trail will continue from A to B but not on to C.
     
    Last edited: Mar 14, 2019
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Did you try disabling the component itself?
     
  3. Happystudio

    Happystudio

    Joined:
    Mar 1, 2015
    Posts:
    4
    Just tried that, has the same effect, when i turn the component back on the trail is still going to position B
     
  4. BakeMyCake

    BakeMyCake

    Joined:
    May 8, 2017
    Posts:
    175
    I am currently dealing with the same exact problem. Did you ever figure this one out? Aside from having another gameobject for the trail that is...
     
    Zethros likes this.
  5. TheLastEinherjar

    TheLastEinherjar

    Joined:
    Jan 11, 2020
    Posts:
    2
    this is some thing I came up with it worked for me I was using an animation event to trigger it

    public bool trailIsOn = false;
    public GameObject trail;


    public void TrailOnOff()
    {
    trailIsOn = !trailIsOn;
    if (trailIsOn == true)
    {
    trail.GetComponent<TrailRenderer>().time = 5f;
    }
    else
    {
    trail.GetComponent<TrailRenderer>().time = 0f;
    }
    }
     
  6. efge

    efge

    Joined:
    Dec 13, 2007
    Posts:
    62
    My solution: Wait one frame after disabling and before enabling:

    Code (CSharp):
    1. trailRenderer.emitting = false;
    2. yield return new WaitForEndOfFrame();
    3. transform.position = pos;
    4. yield return new WaitForEndOfFrame();
    5. trailRenderer.emitting = true;
     
  7. Rafioz0

    Rafioz0

    Joined:
    Dec 31, 2019
    Posts:
    1
  8. Xaron

    Xaron

    Joined:
    Nov 15, 2012
    Posts:
    368
    What worked for me was:

    Code (CSharp):
    1.     float oldDist = trail.minVertexDistance;
    2.     trail.minVertexDistance = 1000000.0f;
    3.     transform.position = pos;
    4.     trail.minVertexDistance = oldDist;
    I know this is more like a hack but the solution from @efge did not work for me.
     
    neonblitzer likes this.