43 lines
1.2 KiB
C#
43 lines
1.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class PathTracer : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private List<Transform> path;
|
|
private Vector3 currTargetPos;
|
|
private int currTargetIndex;
|
|
[SerializeField]
|
|
private float speed;
|
|
[SerializeField][Tooltip("True = Go back to first pos; False = Inverse list")]
|
|
private bool loop = true;
|
|
private Rigidbody rb;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
rb = GetComponent<Rigidbody>();
|
|
rb.position = path[0].position;
|
|
currTargetIndex = 1;
|
|
currTargetPos = path[currTargetIndex].position;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if(Vector3.Distance(transform.position, currTargetPos) <= 0){
|
|
currTargetIndex++;
|
|
if(currTargetIndex >= path.Count){
|
|
if(!loop){
|
|
path.Reverse();
|
|
}
|
|
currTargetIndex = 0;
|
|
}
|
|
|
|
currTargetPos = path[currTargetIndex].position;
|
|
}
|
|
rb.position = Vector3.MoveTowards(transform.position, currTargetPos, speed/100);//Magic number pour avoir une better time in inspector
|
|
}
|
|
|
|
}
|