Trail Renderer SetPosition isn't setting positions properly

Hey everybody,

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 SetPosition. My Unity Version is 2022.3.14f.

Whenever I call SetPosition(), 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 rendering 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 SetPosition() and they’re in the right place.

Here’s a picture of the trail renderer’s visuals after SetPosition 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.

Thanks!

Where / when do you actually update the TrailRenderer’s own gameobject? Where is that even attached to? Have you tried calling Clear in between GetPositions and SetPositions?

Hey buddy, the trail renderer’s gameobject is a child of the plane itself, so moves with the plane. The trail is offset whenever the plane moves too far away from the origin I call a function to shift everything in the scene to move the plane back to the origin to avoid floating point error. During this function, I invoke a UnityEvent which a controller is subscribed to to update the trail renderers which is the function in my first post.

private void LateUpdate()
{
    FloatingOriginUpdate(); //this does not work with trail renderers 
}

void FloatingOriginUpdate()
{
    if (GameManager.PlayerAircraft.transform.position.magnitude > floatingPointResetThreshold)
    {
        sceneObjects = SceneManager.GetActiveScene().GetRootGameObjects();
        origin -= GameManager.PlayerAircraft.transform.position;
        Vector3 offset = -GameManager.PlayerAircraft.transform.position;
        if (FloatingOriginEvent != null)
        {
            FloatingOriginEvent.Invoke(GameManager.PlayerAircraft.transform.position);
        }
        AltosSkyDirector.Instance.SetOrigin(origin);
        for (int i = 0; i < sceneObjects.Length;  i++)
        {
            sceneObjects[i].transform.position += offset;
        }
    }
}

I can’t clear the trail because I need the contrail to hang around for a while for a realistic effect.

same issue here, unity havent implemented a function to shift the line renderer points yet i think