當回圈標志設定為 true 時,它??會繼續在航路點 nonsotp 之間移動。
問題是,當標志回圈設定為 false 時,它??會在最后一個航點處停止,但隨后也會出現例外:ArgumentOutOfRangeException: Index was out of range。必須是非負數且小于集合的大小。
指數在移動功能中不斷上升。當回圈標志為假時,我不確定如何解決它。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Waypoints : MonoBehaviour
{
public float speed;
public bool go = false;
public bool moveToFirstPositionOnStart = false;
public List<Transform> positions;
public bool loop = true;
private int index = 0;
// Start is called before the first frame update
void Start()
{
if (moveToFirstPositionOnStart == true)
{
transform.position = positions[index].position;
}
}
// Update is called once per frame
void Update()
{
if (go == true)
{
Move();
}
}
void Move()
{
transform.position = Vector3.MoveTowards(transform.position,
positions[index].position,
speed * Time.deltaTime);
float distance = Vector3.Distance(transform.position, positions[index].position);
if (distance < 0.01f)
{
positions[index].GetComponent<Renderer>().material.color = Color.red;
index = 1;
}
if (index == positions.Count && loop)
{
index = 0;
}
}
}
uj5u.com熱心網友回復:
Move根據是否loop禁用,使用以下命令在您的航路點序列結束時停止輸入:
if (index == positions.Count)
{
index = 0;
go = loop;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/410961.html
標籤:
