- Home /
How can I change the code so when the object reach the last waypoint it will move in reverse ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveOnCurvedLine : MonoBehaviour
{
public LineRenderer lineRenderer;
public GameObject objectToMove;
public float speed;
public bool go = false;
public bool moveToFirstPositionOnStart = false;
private Vector3[] positions;
private Vector3[] pos;
private int index = 0;
// Start is called before the first frame update
void Start()
{
pos = GetLinePointsInWorldSpace();
if (moveToFirstPositionOnStart == true)
{
objectToMove.transform.position = pos[index];
}
}
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 (go == true)
{
Move();
}
}
void Move()
{
Vector3 newPos = objectToMove.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
{
index = (index + 1) % pos.Length;
}
else
{
stillTraveling = false;
}
}
objectToMove.transform.position = newPos;
}
}
Now when the object that move is reaching the last position he is moving to the first position and then moving over again forward over all the positions.
but instead I want to make that when the object that move is reaching the last position that he will move in reverse on the positions back to the first position and then move forward to the last position and so on in this logic.
I'm not sure what should I change to get what I want in the line :
index = (index + 1) % pos.Length;
Or how to change the loop or add another method with another loop for the reverse.
Can someone show me a solution with a code please ?
I posted as answer what I tried but it's still not working perfect.
Answer by Chocolade · Oct 28, 2020 at 08:02 PM
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveOnCurvedLine : MonoBehaviour
{
public LineRenderer lineRenderer;
public GameObject objectToMove;
public float speed;
public bool go = false;
public bool moveToFirstPositionOnStart = false;
private Vector3[] positions;
private Vector3[] pos;
private int index = 0;
private bool isreverse = false;
// Start is called before the first frame update
void Start()
{
pos = GetLinePointsInWorldSpace();
if (moveToFirstPositionOnStart == true)
{
objectToMove.transform.position = pos[index];
}
}
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 (go == true)
{
Move();
}
if (index == pos.Length - 1)
{
System.Array.Reverse(pos);
isreverse = true;
}
}
void Move()
{
Vector3 newPos = objectToMove.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 (isreverse)
{
if (newPos == pos[index]) // Vector3 comparison is approximate so this is ok
{
index --;// % pos.Length;
}
else
{
stillTraveling = false;
}
}
else
{
if (newPos == pos[index]) // Vector3 comparison is approximate so this is ok
{
index = (index + 1) % pos.Length;
}
else
{
stillTraveling = false;
}
}
}
objectToMove.transform.position = newPos;
}
}
And it's moving in reverse when it's getting to the last position but there is a problem :
Each time it's getting to the last position the object to move is stuck and shaking like it's trying to move back to forward but stay in place and shaking and then after few seconds it's starting moving back in reverse. Why it happens ?
Your answer
![](https://koobas.hobune.stream/wayback/20220613014705im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
AI pause and attack a player in c# (script included) 2 Answers
On waypoints if i set the moving speed of player to 20-30 he fall out of terrain why ? 0 Answers