Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug Trail Renderer SetPosition not actually setting visual positions

Discussion in 'General Graphics' started by Mackerel_Sky, May 6, 2024.

  1. Mackerel_Sky

    Mackerel_Sky

    Joined:
    Jul 24, 2018
    Posts:
    33
    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:
    Code (CSharp):
    1.     void TrailOffset(Vector3 offsetPosition)
    2.     {
    3.         foreach (TrailRenderer trailRenderer in trails)
    4.         {
    5.             int trailSize = trailRenderer.positionCount;
    6.             Vector3[] positions = new Vector3[trailSize];
    7.             trailRenderer.GetPositions(positions);
    8.             if (positions.Length > 0)
    9.             {
    10.                 for (int i = 0; i < positions.Length; ++i)
    11.                 {
    12.                     positions[i] -= offsetPosition;
    13.                 }
    14.             }
    15.             trailRenderer.SetPositions(positions);
    16.  
    17.             trailRenderer.GetPositions(positions);
    18.             //DEBUG
    19.             for (int i = 0; i < positions.Length; ++i)
    20.             {
    21.                 Instantiate(debugObject, positions[i], Quaternion.identity);
    22.             }
    23.         }
    24.     }
    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.
     
    Last edited: May 15, 2024
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,295
  3. Mackerel_Sky

    Mackerel_Sky

    Joined:
    Jul 24, 2018
    Posts:
    33
    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.
     
    Last edited: May 15, 2024
    richardkettlewell likes this.
  4. Mackerel_Sky

    Mackerel_Sky

    Joined:
    Jul 24, 2018
    Posts:
    33