The post is a bit long but all the scripts are connected.
I have a simple waypoints system.
The first script is moving along LineRenderer line/s positions :
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class MoveOnCurvedLines : MonoBehaviour
{
public LineRenderer lineRenderer;
public float speed;
public bool go = false;
public bool moveToFirstPositionOnStart = false;
public float rotSpeed;
public bool random = false;
public int currentCurvedLinePointIndex;
private Vector3() positions;
private Vector3() pos;
private int index = 0;
private bool goForward = true;
private List<GameObject> curvedLinePoints = new List<GameObject>();
private int numofposbetweenpoints;
private bool getPositions = false;
int randomIndex;
int curvedPointsIndex;
// Start is called before the first frame update
void Start()
{
curvedLinePoints = GameObject.FindGameObjectsWithTag("Curved Line Point").ToList();
if (curvedLinePoints != null && curvedLinePoints.Count > 0)
{
transform.rotation = curvedLinePoints(1).transform.rotation;
}
}
Vector3() GetLinePointsInWorldSpace()
{
positions = new Vector3(lineRenderer.positionCount);
//Get the positions which are shown in the inspector
lineRenderer.GetPositions(positions);
//the points returned are in world space
return positions;
}
// Update is called once per frame
void Update()
{
if (lineRenderer.positionCount > 0 && getPositions == false)
{
pos = GetLinePointsInWorldSpace();
numofposbetweenpoints = curvedLinePoints.Count;
if (moveToFirstPositionOnStart == true)
{
transform.position = pos(index);
}
getPositions = true;
}
if (go == true && lineRenderer.positionCount > 0)
{
Move();
}
var dist = Vector3.Distance(transform.position, curvedLinePoints(curvedPointsIndex).transform.position);
if (dist < 0.1f)
{
if (curvedPointsIndex < curvedLinePoints.Count - 1)
curvedPointsIndex++;
currentCurvedLinePointIndex = curvedPointsIndex;
}
}
int counter = 0;
int c = 1;
void Move()
{
Vector3 newPos = transform.position;
float distanceToTravel = speed * Time.deltaTime;
bool stillTraveling = true;
while (stillTraveling)
{
Vector3 oldPos = newPos;
newPos = Vector3.MoveTowards(oldPos, pos(index), distanceToTravel);
distanceToTravel -= Vector3.Distance(newPos, oldPos);
if (newPos == pos(index)) // Vector3 comparison is approximate so this is ok
{
// when you hit a waypoint:
if (goForward)
{
bool atLastOne = index >= pos.Length - 1;
if (!atLastOne)
{
index++;
counter++;
if (counter == numofposbetweenpoints)
{
c++;
counter = 0;
}
if (c == curvedLinePoints.Count - 1)
{
c = 0;
}
}
else { index--; goForward = false; }
}
else
{ // going backwards:
bool atFirstOne = index <= 0;
if (!atFirstOne)
{
index--;
counter++;
if (counter == numofposbetweenpoints)
{
c++;
counter = 0;
}
if (c == curvedLinePoints.Count - 1)
{
c = 0;
}
}
else { index++; goForward = true; }
}
}
else
{
stillTraveling = false;
}
}
transform.position = newPos;
}
}
The second script is creating the lines :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GenerateLines : MonoBehaviour
{
public GameObject linesWaypointsPrefab;
public int amountOfLines = 30;
public int minRandRange, maxRandRange;
public bool randomPositions = false;
public bool generateNewPositions = false;
private List<Vector3> linesWaypoints = new List<Vector3>();
private Transform waypointsLinesParent;
// Start is called before the first frame update
void Awake()
{
waypointsLinesParent = GameObject.Find("Curved Lines").transform;
if (generateNewPositions || (linesWaypoints.Count == 0 && amountOfLines > 0))
{
GenerateLinesWaypoints();
}
}
// Update is called once per frame
void Update()
{
}
private void GenerateLinesWaypoints()
{
for (int i = 0; i < amountOfLines; i++)
{
if (randomPositions)
{
var randPosX = UnityEngine.Random.Range(minRandRange, maxRandRange);
var randPosY = UnityEngine.Random.Range(minRandRange, maxRandRange);
var randPosZ = UnityEngine.Random.Range(minRandRange, maxRandRange);
if(linesWaypointsPrefab != null)
{
var LineWaypoint = Instantiate(linesWaypointsPrefab,
new Vector3(randPosX, randPosY, randPosZ), Quaternion.identity, waypointsLinesParent);
LineWaypoint.name = "Curved Line Point";
LineWaypoint.tag = "Curved Line Point";
}
}
else
{
if (linesWaypointsPrefab != null)
{
var LineWaypoint = Instantiate(linesWaypointsPrefab,
new Vector3(i, i, i), Quaternion.identity, waypointsLinesParent);
LineWaypoint.name = "Curved Line Point";
LineWaypoint.tag = "Curved Line Point";
}
}
}
}
}
And last the script that should update in real time in run time the curved line/s positions and the lines positions between each curved point :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
(RequireComponent( typeof(LineRenderer) ))
public class CurvedLineRenderer : MonoBehaviour
{
//PUBLIC
public float lineSegmentSize = 0.15f;
public float lineWidth = 0.1f;
(Tooltip("Enable this to set a custom width for the line end"))
public bool useCustomEndWidth = false;
(Tooltip("Custom width for the line end"))
public float endWidth = 0.1f;
(Header("Gizmos"))
public bool showGizmos = true;
public float gizmoSize = 0.1f;
public Color gizmoColor = new Color(1,0,0,0.5f);
//PRIVATE
private CurvedLinePoint() linePoints = new CurvedLinePoint(0);
private Vector3() linePositions = new Vector3(0);
private Vector3() linePositionsOld = new Vector3(0);
// Update is called once per frame
public void Update ()
{
if (ResetLineRendererPositions.hasReseted == false)
{
GetPoints();
SetPointsToLine();
}
}
public void GetPoints()
{
//find curved points in children
linePoints = this.GetComponentsInChildren<CurvedLinePoint>();
//add positions
linePositions = new Vector3(linePoints.Length);
for (int i = 0; i < linePoints.Length; i++)
{
linePositions(i) = linePoints(i).transform.position;
}
}
public void SetPointsToLine()
{
//create old positions if they dont match
if( linePositionsOld.Length != linePositions.Length )
{
linePositionsOld = new Vector3(linePositions.Length);
}
//check if line points have moved
bool moved = false;
for( int i = 0; i < linePositions.Length; i++ )
{
//compare
if( linePositions(i) != linePositionsOld(i) )
{
moved = true;
}
}
//update if moved
if( moved == true )
{
LineRenderer line = this.GetComponent<LineRenderer>();
//get smoothed values
Vector3() smoothedPoints = LineSmoother.SmoothLine( linePositions, lineSegmentSize );
//set line settings
line.positionCount = smoothedPoints.Length;
line.SetPositions( smoothedPoints );
line.startWidth = lineWidth;
line.endWidth = useCustomEndWidth ? endWidth : lineWidth;
}
}
void OnDrawGizmosSelected()
{
Update();
}
void OnDrawGizmos()
{
if( linePoints.Length == 0 )
{
GetPoints();
}
//settings for gizmos
foreach( CurvedLinePoint linePoint in linePoints )
{
linePoint.showGizmo = showGizmos;
linePoint.gizmoSize = gizmoSize;
linePoint.gizmoColor = gizmoColor;
}
}
}
The result in the end is some cubes in this case 5 that are connected with lines and a transform that move on the positions of the lines not between the curved points(Cubes) but moving between the positions of the lines :

The problem is with the last script. When the game is running and then when I’m selecting one of the curved points(Cube) and drag it around in the scene window I see it changing it’s position and the line also change but in fact it’s not changing the positions of the line and the position of the curved point in the moving script the first script.
The moving platform is keep moving on the old positions as before and never moving on the new positions.
In this screenshot I moved the fourth curved point(Cube) dragged it around to the right top but the platform is still moving on the old line positions of the fourth curved point(Cube) position it was before I moved it.

I want to do that when I change the curved points(Cubes) and it’s changing also the lines positions that it will update the transform that should move on this positions.
If I’m not mistaken the last script should do it but it’s not.
In the last script in the Update hasReseted is false all the time :
public void Update ()
{
if (ResetLineRendererPositions.hasReseted == false)
{
GetPoints();
SetPointsToLine();
}
}
even if I set for a second the hasReseted to true and then to false again and it’s clearing the LineRenderer all lines and positions and re generating them over again the transform platform will keep moving on the old positions.