Trail Renderer SetPosition not actually setting visual positions

Hi guys,

I’m been trying to get a trail renderer to work with a floating origin system I’m implementing and pretty sure I’ve come across a bug in SetPositions. My Unity Version is 2022.3.14f.

Whenever I call SetPositions(), the trail renderer seems to be correctly setting the positions of all the vertices, but the visual trail itself doesn’t seem to be correctly displaced. I think the trail renderer is still renderering the trail at the previous position, making it incorrectly renderer a line at that point to its new position. You can see the effect on the image below:

I debugged this by spawning an object at each of the trail vertices after SetPositions() and they’re in the right place.

Here’s a picture of the trail renderer’s visuals after SetPositions is called. They’re in their original position - i.e. not having been shifted along with the vertices.

Here’s my offset function:

    void TrailOffset(Vector3 offsetPosition)
    {
        foreach (TrailRenderer trailRenderer in trails)
        {
            int trailSize = trailRenderer.positionCount;
            Vector3[] positions = new Vector3[trailSize];
            trailRenderer.GetPositions(positions);
            if (positions.Length > 0)
            {
                for (int i = 0; i < positions.Length; ++i)
                {
                    positions[i] -= offsetPosition;
                }
            }
            trailRenderer.SetPositions(positions);

            trailRenderer.GetPositions(positions);
            //DEBUG
            for (int i = 0; i < positions.Length; ++i)
            {
                Instantiate(debugObject, positions[i], Quaternion.identity);
            }
        }
    }

Has anybody run into something similar or knows if this is intended behaviour? Really would appreciate any help there is, as this is the last thing I need to fix up before a floating point implementation is good to go.

Does the docs page help? It describes some limitations.

Have you debugged offsetPosition to make sure it’s not zero?
Have you printed out some positions before + after the modification.
Debug.Log ought to show if something weird is happening, and where the weird part is.

Hey Rich, thanks for the suggestions. Here’s what I’ve done:

  1. Debugged offsetPosition and confirmed it is not zero. All of my other scene objects are also being offset by the correct amount.
  2. Printed the new positions after I call SetPositions() or SetPosition() (neither method works) and confirm they’re being set to the right positions.
  3. Spawned those debug objects in the 2nd screenshot after calling SetPositions() and confirmed they were being moved to the right position.

Following this, I replicated the trail effect using a line renderer as the docs say they use the same rendering algorithm and duplicated the debug steps I described above. The line renderer inspector shows the vertices, however, and I can see that they aren’t actually being set despite what the Debug.Log says.

Below, I’m moving the renderer position up by 10 (from -82.19), printed new positions in Debug.Log and see that all the vertices are being moved up by 10.

Checking the inspector:

The random numbers at the front are the trails catching up with the moving object in my test scene. All of these numbers should be -72.19 if SetPositions() works like how I expect it to.

I set up a completely empty project and replicated the issue, so I’ve submitted a bug report under CASE IN-75751.

1 Like

This bug has been verified! If anybody is running into the same problem or would like to help me out, please give it a vote to priortise!

1 Like

So the issue got marked as Won’t Fix… but before that there was some attention from developers saying it could have been related to a different issue that was fixed in 2022.3.36 - I downloaded that build and the problem seems to have gone away! Guess it all worked out. Many thanks to the Unity devs.

1 Like