Waypoint based AI problems, can't set approriate target (Javascript)

Okay, so I’ve been trying to set up a bit of code, that will set a target, in this case WayA (an empty game object with a trigger), move to that target, then once it hits the sphere collider with the tag WayA, it sets the waypoint target to WayB, and so on, however, this doesn’t seem to work to plan, perhaps it’s because of the Transform section of the code, would it work if I used Transform as itself like the waypoint var is? I’ve set up the tags correctly,aswell as sphere colliders on all the waypoints, I just can’t quite figure out what it is I’m doing wrong…

Any help on this would be awesome, thanks :slight_smile:

#pragma strict

var waypoint: Transform;
var speed: float;

var wayA: Transform [];
var wayB: Transform [];
var wayC: Transform [];
var wayD: Transform [];
var wayE: Transform [];
var wayF: Transform [];
var wayG: Transform [];
var wayH: Transform [];
var wayI: Transform [];
var wayJ: Transform [];
var wayK: Transform [];
var wayL: Transform [];
var wayM: Transform [];
var wayN: Transform [];
var wayO: Transform [];
var wayEnd: Transform [];


function Start () {

waypoint == wayA;
}

function Update () {
}
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == "WayA")
{
waypoint == wayB;
}


if(other.gameObject.tag == "WayB")
{
waypoint == wayC;
}


if(other.gameObject.tag == "WayC")
{
waypoint == wayD;
}


if(other.gameObject.tag == "WayD")
{
waypoint == wayE;
}


if(other.gameObject.tag == "WayE")
{
waypoint == wayF;
}


if(other.gameObject.tag == "WayF")
{
waypoint == wayG;
}


if(other.gameObject.tag == "WayG")
{
waypoint == wayH;
}


if(other.gameObject.tag == "WayH")
{
waypoint == wayI;
}


if(other.gameObject.tag == "WayI")
{
waypoint == wayJ;
}


if(other.gameObject.tag == "WayJ")
{
waypoint == wayK;
}


if(other.gameObject.tag == "WayK")
{
waypoint == wayL;
}


if(other.gameObject.tag == "WayL")
{
waypoint == wayM;
}


if(other.gameObject.tag == "WayM")
{
waypoint == wayN;
}


if(other.gameObject.tag == "WayN")
{
waypoint == wayO;
}


if(other.gameObject.tag == "WayO")
{
waypoint == wayEnd;
}
{
var step = speed * Time.deltaTime;

transform.position = Vector3.MoveTowards(transform.position, waypoint.position, step);
}
}

Hi

You are complicating stuff there is an easier ways of doing that using Arrays

public var Waypoints:Transform[]; // Collection of Waypoints

public var speed:float;

public var minDist:float;

var waypIndex:int =0;
function Update()
{
    if(Vector3.Distance(Waypoints[waypIndex] - transform.position) > minDist)
   {
   transform.position = Vector3.Lerp(transform.position,Waypoints[index],Time.deltaTime * speed);
  }else{
  if(waypIndex++ < Waypoints.Lenght) 
   waypIndex++; // increase idex by 1
 else
  waypIndex = Waypoints.Lenght -1; // Set the index as the last index 
 
}


}

if you dont understand how it works please do leave a comment

It’s cool, I solved it xD

I turned it into 2 seperate sections of code, I realized I had the update section set up wrong on the original, so I’ll post the 2 scripts if anyone’s trying to figure out the same thing.

#pragma strict

var waypoint: Transform;

var wayA: Transform;
var wayB: Transform;
var wayC: Transform;
var wayD: Transform;
var wayE: Transform;
var wayF: Transform;
var wayG: Transform;
var wayH: Transform;
var wayI: Transform;
var wayJ: Transform;
var wayK: Transform;
var wayL: Transform;
var wayM: Transform;
var wayN: Transform;
var wayO: Transform;
var wayEnd: Transform;


function Start () {

waypoint = wayA.transform;
}
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == "WayA")
{
waypoint = wayB.transform;
}


if(other.gameObject.tag == "WayB")
{
waypoint = wayC.transform;
}


if(other.gameObject.tag == "WayC")
{
waypoint = wayD.transform;
}


if(other.gameObject.tag == "WayD")
{
waypoint == wayE.transform;
}


if(other.gameObject.tag == "WayE")
{
waypoint = wayF.transform;
}


if(other.gameObject.tag == "WayF")
{
waypoint = wayG.transform;
}


if(other.gameObject.tag == "WayG")
{
waypoint = wayH.transform;
}


if(other.gameObject.tag == "WayH")
{
waypoint = wayI.transform;
}


if(other.gameObject.tag == "WayI")
{
waypoint = wayJ.transform;
}


if(other.gameObject.tag == "WayJ")
{
waypoint = wayK.transform;
}


if(other.gameObject.tag == "WayK")
{
waypoint = wayL.transform;
}


if(other.gameObject.tag == "WayL")
{
waypoint = wayM.transform;
}


if(other.gameObject.tag == "WayM")
{
waypoint = wayN.transform;
}


if(other.gameObject.tag == "WayN")
{
waypoint = wayO.transform;
}


if(other.gameObject.tag == "WayO")
{
waypoint = wayEnd.transform;
}

}

#pragma strict
class AITest_2 extends AI_Test
{
// The target marker.
	var target: Transform;
	
	// Speed in units per sec.
	var speed: float;
	
	
	function Update () {
		// The step size is equal to speed times frame time.
		var step = speed * Time.deltaTime;
		
		var target = waypoint;
		
		// Move our position a step closer to the target.
		transform.position = Vector3.MoveTowards(transform.position, target.position, step);
	}
	}