Line tracing a user controller objects movement

Hey

Looking for some starting advice / potential existing solutions for this problem.

I’m trying to trace a line that follows the users controlled object position, from a start point - like tracing a trail behind pac man for example from the start point. What I’m stuck on is if the player decides to change direction and move back on itself - what would be the best way to update the line renderer so that you start to delete the most recently drawn points rather than keep adding new ones?

I want to only draw the line that traces the players movement route but only to the player and never beyond if they reverse and go back where they came.

A good example I found was the puzzle ui in ‘The Witness’ which does essentially the same thing - if you back track/reverse, it doesnt add more line to show that reversal, it just removes the paths you’re undoing, only drawing the route between the start point and the object.

Suggestions much appreciated!

Edit: thinking out loud:

Would I draw lines between junction points, and then the most recently passed junction to the player? So only the line between the player and the most recently point is ‘live’ and can be undone?

You could use the line renderer for this I suppose, I don’t have much experience with that, but it could work.

You’ll need to check if each new line segment intersects with another in the trail, and if it does, then clear everything beyond the intersected segment, and then connect that segment’s base to your newest point to continue the line.

I don’t know what kind of range of movement your character has, but if it’s not based on intersection and simply moving Positive-X or Negative-X direction, you could make it so any time your player’s X position is less than the last point in the line, you remove that point. Anytime your player’s X is “some distance” greater than the last point, you draw a new point and segment.