i’m attempting to get the AI unit within my game to fist find any flags within the scene and collect them and then once all flags are collected move to exit its NavMeshAgent based AI
here’s my script so far any help would be great
private var agent : NavMeshAgent;
private var GameObject;
var flag:Transform;
var exit:Transform;
var flagcount = 1;
private var gos : GameObject[];
private var ext : GameObject[];
function Start(){
//var gos : GameObject[];
// var ext : GameObject[];
agent = GetComponent.<NavMeshAgent>();
//eninmeGameObject = GameObject.FindWithTag ( "eventtile" );
gos = gameObject.FindGameObjectsWithTag("flag");
ext = gameObject.FindGameObjectsWithTag("exit");
if (gos.length == 0) {
Debug.Log("No game objects are tagged with feg");
}
if (ext.length == 0) {
Debug.Log("No game objects are tagged with feg");
}
}
function Update(){
//agent.SetDestination(goal.transform.position);
if(flagcount >= 0){
if (gos.length >=0 ) {
agent.destination = flag.position;
}
}
else
if(flagcount == 0){
if (gos.length == 0) {
if (ext.length >= 0) {
agent.destination = exit.position;
}
}
}
}
function OnTriggerEnter (other : Collider) {
if (other.gameObject.tag == "flag"){
other.gameObject.active = false;
flagcount-=1;
}
}
line 43, you state, gos.length == 0, remove that if from your logic. gos, which is a predetermined list of objects will never be equal to 0. Since you never remove things from that list, it’s count will never change.
For that matter:
right after line 15, set the flagcount to gos.Length
remove the if at line 33 as it is not necessary.
i’ve made the changes ass suggested and it still only goes for one flag and does not move to exit
Untested, but this is a cleaner representation of what you want.
private var agent : NavMeshAgent;
private var GameObject;
var flags : GameObject[];
var target : GameObject;
var exit : GameObject;
private index : int = 0;
private var lastTarget : GameObject;
function Start() {
// get the current agent
agent = GetComponent. < NavMeshAgent > ();
// store the flags
flags = gameObject.FindGameObjectsWithTag("flag");
// if we dont have enough, just quit
if (flags.length == 0) {
Debug.Log("No game objects are tagged with feg");
}
// find the first exit and store it
var exits = gameObject.FindGameObjectsWithTag("exit");
if (exits.length == 0) {
Debug.Log("No game objects are tagged with feg");
} else {
exit = exits[0];
}
// set the first target
SetTarget();
}
function Update() {
// if we have no target for any reason, quit
if(target == null){ return; }
// set the target to the next point we need to go to.
SetTarget();
// if that point is not the same as the last one, we need to update the destination.
if(lastTarget != target){
agent.destination = target.transform.position;
}
// store the curren target, so we see if we have changed frame to frame
lastTarget = target;
// if we are within 3 units of a flag, or the exit, get the next point to go.
if((target.transform.position - transform.position).sqrMagnitude < 9){
index++;
}
}
// this will get the target from the current index
function SetTarget(){
// set the target to null
target = null;
// if we are above the count + the exit, just return to stop more from happening
if(index > flags.Length) return;
// if we are equal to the number of flags, exit
if(index == flags.Length){
target = exit;
} else {
// if not, just pick that flag
target = flags[index];
}
}
well its working a lot better then the way i was going but its not picking up the flags it gets near them and then goes to exit