Hey guys bring to make a simple waypoint system so my spaceship dose loop the loops and u-turns, buy
following game objects in GameObject and then goes to the next game object in the group, but I’m getting Index is out of range…
using UnityEngine;
using System.Collections;
public class Manovers : MonoBehaviour {
public GameObject LoopTheLoop;
public GameObject uTurn;
public GameObject[] LoopPoints;
public GameObject[] TurnPoints;
public GameObject Target;
public PlayerControllerWizards Controlls;
public int CurrentTarget = 0;
public int MaximumTargets;
public float dist;
public int Manoover;
public float turnspeed = 20f;
public bool InManoover = false;
// Use this for initialization
void Start () {
Controlls = this.gameObject.GetComponent<PlayerControllerWizards> ();
}
// Update is called once per frame
void Update () {
if (Input.GetButton ("ManoverOne") && InManoover == false) {
InManoover = true;
GameObject Clone;
Clone = Instantiate (LoopTheLoop, this.transform.position, transform.rotation) as GameObject;
Manoover = 1;
}
if (Input.GetButton ("ManoverTwo") && InManoover == false) {
InManoover = true;
GameObject Clone;
Clone = Instantiate (uTurn, this.transform.position, transform.rotation) as GameObject;
Controlls.enabled = false; // Disable controlls while in manoover;
Manoover = 2;
}
// perform Loop the Loop
if (Manoover == 1) {
MaximumTargets = LoopPoints.Length;
Target = (LoopPoints[CurrentTarget]);
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (Target.transform.position - transform.position), Time.deltaTime * turnspeed);
if(Target != null && dist < 2){
Target = (LoopPoints[CurrentTarget +=1 ]);
}
if (CurrentTarget > MaximumTargets) {
Controlls.enabled = true;
Manoover = 0;
InManoover = false;
Destroy(LoopTheLoop);
}
}
// perform Utruns
if (Manoover == 2) {
MaximumTargets = TurnPoints.Length;
Target = (TurnPoints[CurrentTarget]);
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation (Target.transform.position - transform.position), Time.deltaTime * turnspeed);
if(Target != null && dist < 2){
Target = (TurnPoints[CurrentTarget +=1 ]);
}
if (CurrentTarget > MaximumTargets) {
Controlls.enabled = true;
Manoover = 0;
InManoover = false;
Destroy(uTurn);
}
}
}
}