Trail Renderer leaves trail after teleport

Hi guys, in my game, im trying to teleport 2 balls with my script that have attached Trail renderers, and when i move them somewhere they leave a trail after them, i have tried to use various solutions, but non of them worked even they should have.

here is my Update method:

    void Update()
        {   
            if (Mathf.Abs(timer.GetComponent<NumberUpdater>().elapsedTime % 65) < 0.0101f)
            {
                if (!jsonCounterUpdated)
                {sp.isMatchActive = false;}
                jsonCounterUpdated = true; // Prevent multiple updates
            }
            else
            {
                jsonCounterUpdated = false; // Reset the flag when the condition is not met
            }
      
            if(!sp.isMatchActive)
            {   
                sp.isMatchActive = true;
                sp.jsonDataCounter ++;
                // Update the teams in Scene Properties
                UpdateActiveVar();
                // TODO - Reset UI (Hook, comment1, comment2, (CTAs (Follow, Repost)))
      
                // Reset score logic 
                ResetScore();
                // Reset timer logic
                ResetTimer();
                // Set/Reset Ballz/Spawn logic
                // TODO - set the velocity = 0
                SetBallz();
                // Set/Reset Logos logic
                SetLogos();
                // Reset Ring 
                ResetRing();
                // Reset/Destory Audio
                ResetAndDestroyAudio();
                // Disable animations
                //  Goal
                DisableGoalAnim();
                //  Fans
                DisableFansAnim();
                //  Logos
                DisableLogosAnim();
                // Reset UI
                //  WAITING: Hook from Krystof
                //  comment 1
                //  comment 2
                // CTAs
                //  Follow
                //  Repost
            }
        }

I the method where i reset the ball the SetBallz():

void SetBallz()
          {
              // Set color and logo to the right Team
              rightBall = GameObject.FindGameObjectWithTag("Ball");
              rightBall.GetComponent<SpriteRenderer>().color = sp.rightActiveTeam.teamColor;
              GameObject rightTeamSprite = rightBall.transform.GetChild(rightBall.transform.childCount - 1).gameObject;
              rightTeamSprite.GetComponent<SpriteRenderer>().sprite = sp.rightActiveTeam.teamLogo;

              // Set color and logo to the left Team
              leftBall = GameObject.FindGameObjectWithTag("Ball1");
              leftBall.GetComponent<SpriteRenderer>().color = sp.leftActiveTeam.teamColor;
              GameObject leftTeamSprite = leftBall.transform.GetChild(leftBall.transform.childCount - 1).gameObject;
              leftTeamSprite.GetComponent<SpriteRenderer>().sprite = sp.leftActiveTeam.teamLogo;

              // Set the cordinates of the ballz
              GenerateRandomPositions(xMin,xMax,yMin,yMax);
              rightBall.GetComponent<TrailRenderer>().emitting = false;
              leftBall.GetComponent<TrailRenderer>().emitting = false;
              rightBall.transform.position = new Vector3(randomX, randomY, transform.position.z);
              leftBall.transform.position = new Vector3(-randomX, randomY, transform.position.z);
              rightBall.GetComponent<TrailRenderer>().emitting = true;
              leftBall.GetComponent<TrailRenderer>().emitting = true;
          }

Or i have tried to use

leftBall.setActive(false);
rightBall.setActive(false);
rightBall.transform.position = new Vector3(randomX, randomY, transform.position.z);
leftBall.transform.position = new Vector3(-randomX, randomY, transform.position.z);
leftBall.setActive(true);
rightBall.setActive(true);

But it did not work as well, and i have no idea what am I doing wrong please help me

I had trouble shutting off the TrailRenderer from noticing where I was moving.

I was trying to recycle it again and again on a tank that fired a single bullet, then I would set the bullet inactive and return it to the tank.

I could never get that to work 100% so in the end I just Destroy() the TrailRenderer before teleporting, then recreate it when I arrive at the new position.

1 Like

i have maybe found a solution,

In my code it looks like this, and it works, but i think i have done maybe too much but it works, for me at least for now

i have turned of the trail renderer component and cleared the trail it self, when i just turn of the component it didnt work, but when i also cleared it, it worked, i hope it will help you as well

void SetBallz()
    {
        // Set color and logo to the right Team
        rightBall = GameObject.FindGameObjectWithTag("Ball");
        rightBall.GetComponent<SpriteRenderer>().color = sp.rightActiveTeam.teamColor;
        GameObject rightTeamSprite = rightBall.transform.GetChild(rightBall.transform.childCount - 1).gameObject;
        rightTeamSprite.GetComponent<SpriteRenderer>().sprite = sp.rightActiveTeam.teamLogo;
        // Set color and logo to the left Team
        leftBall = GameObject.FindGameObjectWithTag("Ball1");
        leftBall.GetComponent<SpriteRenderer>().color = sp.leftActiveTeam.teamColor;
        GameObject leftTeamSprite = leftBall.transform.GetChild(leftBall.transform.childCount - 1).gameObject;
        leftTeamSprite.GetComponent<SpriteRenderer>().sprite = sp.leftActiveTeam.teamLogo;
        
        // Get the the TrailRenderers
        var rightTrail = rightBall.GetComponent<TrailRenderer>();
        var leftTrail = leftBall.GetComponent<TrailRenderer>();
        
        // Clear the Trail
        rightTrail.Clear();
        leftTrail.Clear();

        // Temporarily disable the TrailRenderers
        rightTrail.enabled = false;
        leftTrail.enabled = false;

        // Set the cordinates of the ballz
        GenerateRandomPositions(xMin,xMax,yMin,yMax);

        // Set the velocity = 0
        rightBall.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
        leftBall.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
        
        // Teleport both ballz 
        rightBall.transform.position = new Vector3(randomX, randomY, transform.position.z);
        leftBall.transform.position = new Vector3(-randomX, randomY, transform.position.z);

        // Anable the TrailRenderers
        rightTrail.enabled = true;
        leftTrail.enabled = true;
    }
2 Likes