所以我嘗試着讓一個遊戲對象在三點之間來回移動,這是在C#中的統一,我在統一檢查器中分配了3個遊戲對象,我想讓遊戲對象在點之間來回移動問題是我得到一個索引超出範圍錯誤。爲什麼是這樣的,我該如何解決它? 對不起,可能是格格不入的mestakes。它爲什麼會拋出索引超出範圍錯誤?
這裏是我的代碼:
public class Enamy2 : MonoBehaviour {
public Transform[] pointPosition;
public float enamySpeed;
private int currentPoint;
private bool backTracking = false;
// Use this for initialization
void Start() {
transform.position = pointPosition [0].position;
currentPoint = 0;
}
// Update is called once per frame
void Update() {
if (transform.position == pointPosition[currentPoint].position) {
if (backTracking)
currentPoint--;
else
currentPoint++;
}
if(currentPoint >= pointPosition.Length) {
backTracking = true;
}
transform.position = Vector3.MoveTowards (transform.position, pointPosition [currentPoint].position, Time.deltaTime * enamySpeed);
}
}
我應該如何修改腳本,以便我能得到desiered的結果?我只做了一個星期的團結和C#,所以對我來說很難... –
您需要在if語句中重置'currentPoint'。例如:'backTracking = true; currentPoint = 0;'或者你處理backTracking的方式不同。 –
其實,你也必須確保'currentPoint'從不否定。添加一個if語句來關閉backTracking,如果'currentPoint'達到0. –