Question by
MuhamadBanguzI1993 · Apr 18 at 10:17 PM ·
c#move an objectwaypointwaypointswaypoint system
Why the loop in waypoints in not working when set to true?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class MoveOnCurvedLines : MonoBehaviour
{
public LineRenderer lineRenderer;
public float speed;
public bool go = false;
public bool moveToFirstPositionOnStart = false;
public float rotSpeed;
public bool loop = false;
public bool random = false;
public int currentCurvedLinePointIndex;
public TextMeshProUGUI lastWaypointText;
public TextMeshProUGUI countWaypointText;
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;
}
if (random)
GetNewRandomIndex();
if (lastWaypointText != null)
{
lastWaypointText.text = "Last Waypoint : " + (curvedPointsIndex + 1).ToString();
}
if (countWaypointText != null)
{
countWaypointText.text = "Waypoints Count : " + curvedLinePoints.Count.ToString();
}
}
Vector3[] GetLinePointsInWorldSpace()
{
if (lineRenderer != null)
{
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 != null)
{
if(curvedLinePoints.Count > 0)
{
for(int i = 0; i < curvedLinePoints.Count; i++)
{
if(curvedLinePoints[i].transform.hasChanged)
{
getPositions = false;
}
}
}
if (lineRenderer.positionCount > 0 && getPositions == false)
{
pos = GetLinePointsInWorldSpace();
numofposbetweenpoints = pos.Length / curvedLinePoints.Count;
if (moveToFirstPositionOnStart == true)
{
transform.position = pos[index];
}
getPositions = true;
}
if (go == true && lineRenderer.positionCount > 0)
{
Move();
Vector3 targetDirection = (curvedLinePoints[c].transform.position - transform.position).normalized;
curvedLinePoints[c].transform.localRotation = Quaternion.LookRotation(targetDirection);
transform.localRotation = Quaternion.RotateTowards(transform.localRotation, curvedLinePoints[c].transform.localRotation, Time.deltaTime * rotSpeed);
}
if (curvedLinePoints.Count > 1)
{
var dist = Vector3.Distance(transform.position, curvedLinePoints[curvedPointsIndex].transform.position);
if (dist < 0.1f)
{
if (curvedPointsIndex < curvedLinePoints.Count - 1)
{
curvedPointsIndex++;
if (lastWaypointText != null)
{
lastWaypointText.text = "Last Waypoint : " + curvedPointsIndex.ToString();
}
}
else
{
if (lastWaypointText != null)
{
lastWaypointText.text = "Last Waypoint : " + (curvedPointsIndex + 1).ToString();
}
curvedPointsIndex = 0;
}
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;
}
void GetNewRandomIndex()
{
randomIndex = UnityEngine.Random.Range(0, curvedLinePoints.Count);
}
}
When i set the loop flag to true then in the Move() function it should keep moving to the last waypoint and then to start from the first one. if the loop is false it's working fine the object move between the waypoint when getting to the last its moving backward.
The problem is when the loop is true the object stay on place on the current waypoint and never continue.
This is what I tried in the Move() function but this make the whole game freezing :
bool atLastOne = index >= pos.Length - 1;
if (!atLastOne)
{
index++;
counter++;
if (counter == numofposbetweenpoints)
{
c++;
counter = 0;
}
if (c == curvedLinePoints.Count - 1)
{
c = 0;
}
}
else
{
if (loop == false)
{
index--;
goForward = false;
}
}
Comment