- Home /
How can i loop my path instead of it ending on the lasat point
I have two scripts in C#...
My "path definition"...
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PathDefinition : MonoBehaviour
{
public Transform[] Points;
public IEnumerator<Transform> GetPathEnumerator()
{
if (Points == null || Points.Length < 1)
yield break;
var direction = 1;
var index = 0;
while (true)
{
yield return Points[index];
if(Points.Length == 1)
continue;
if (index <= 0)
direction = 1;
index = index + direction;
}
}
public void OnDrawGizmos()
{
if (Points == null || Points.Length < 2)
return;
for (var i = 1; i < Points.Length; i++)
{
Gizmos.DrawLine(Points[i - 1].position, Points[i].position);
}
}
and my "follow path"....
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class FollowPath : MonoBehaviour
{
public enum FollowType
{
MoveTowards,
Lerp
}
public FollowType Type = FollowType.MoveTowards;
public PathDefinition Path;
public float Speed = 1;
public float MaxDistanceToGoal = .1f;
private IEnumerator<Transform> _currentPoint;
public void Start()
{
if (Path == null)
{
Debug.LogError("Path cannot be null", gameObject);
return;
}
_currentPoint = Path.GetPathEnumerator();
_currentPoint.MoveNext();
if (_currentPoint.Current == null)
return;
transform.position = _currentPoint.Current.position;
}
public void Update()
{
if (_currentPoint == null || _currentPoint.Current == null)
return;
if (Type == FollowType.MoveTowards)
transform.position = Vector3.MoveTowards(transform.position, _currentPoint.Current.position, Time.deltaTime * Speed);
else if (Type == FollowType.Lerp)
transform.position = Vector3.Lerp(transform.position, _currentPoint.Current.position, Time.deltaTime * Speed);
var distanceSquared = (transform.position - _currentPoint.Current.position).sqrMagnitude;
if (distanceSquared < MaxDistanceToGoal * MaxDistanceToGoal)
_currentPoint.MoveNext();
}
How can I make the path so my object moves from point 'a, b, c' then loops not just end on point 'c' and gives me the error 'IndexOutOfRangeException: Array index is out of range'
I'm new to coding to an answer would be great! :)
Answer by slavo · Aug 28, 2014 at 04:54 PM
You need to check that you are at the end of array. Array are indexed from 0 in c#.
if (index <= 0)
direction = 1;
if (index == Points.Length - 1)
{
index = 0;
}
else
{
index = index + direction;
}
Bit off topic, but use of Corutines to get you next point is Overengineering. They are mostly use if your function need to run in multiple frames.
You can achive same behaviour with this script:
using UnityEngine;
using System;
public class FollowPath : MonoBehaviour
{
public Transform[] Points;
public enum FollowType
{
MoveTowards,
Lerp
}
public FollowType Type = FollowType.MoveTowards;
public float Speed = 1;
public float MaxDistanceToGoal = .1f;
private int _currentPointIndex;
private int _direction;
public void Start()
{
if (Points.Length > 1 == false)
{
Debug.LogError("You must specify at least 2 points for path", gameObject);
return;
}
_direction = 1;
_currentPointIndex = 0;
transform.position = Points[0].position;
}
public void Update()
{
if (Points.Length > 1 == false)
return;
if (Type == FollowType.MoveTowards)
transform.position = Vector3.MoveTowards(transform.position, Points[_currentPointIndex].position, Time.deltaTime * Speed);
else if (Type == FollowType.Lerp)
transform.position = Vector3.Lerp(transform.position, Points[_currentPointIndex].position, Time.deltaTime * Speed);
int nextPointIndex = GetNextPointIndex();
float distanceToGoal = (Points[nextPointIndex].position - transform.position).sqrMagnitude;
if (distanceToGoal < MaxDistanceToGoal * MaxDistanceToGoal )
{
_currentPointIndex = nextPointIndex;
}
}
public void OnDrawGizmos()
{
if (Points == null || Points.Length < 2)
return;
for (var i = 1; i < Points.Length; i++)
{
Gizmos.DrawLine(Points[i - 1].position, Points[i].position);
}
}
private int GetNextPointIndex()
{
//check if you are at end of array, go to start if yes
if (_currentPointIndex == Points.Length - 1)
return 0;
//else return next point
return _currentPointIndex + 1;
}
}
Its pseudo code, so it can contains some runtime errors.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Making a block move when its clicked 1 Answer
iOS Tap to move?? 2 Answers
Mathf.SmoothStep not quite working. 1 Answer