So this script is for 5 different AI that basically go from a vending machine to their desk but i want them to come back again in the same order they went there. Each AI has about 5 nodes, i’ve tried a few things but yea its driving me nuts…
var forwardSpeed : float= 1;
public var waypoint : Transform[];
private var pointA : Vector3;
private var currentWaypoint : int;
function Update () {
if(currentWaypoint < waypoint.length){
var target: Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if(moveDirection.magnitude <1){
currentWaypoint++;
}
else{
velocity = moveDirection.normalized * forwardSpeed;
}
rigidbody.velocity = velocity;
}
}
I have tested this and it is working. I included a variable to change whether the waypoint count is going up or down (wayDir), then 2 checks if currentWaypoint > waypoint.length or < 0.
#pragma strict
var forwardSpeed : float = 1;
public var waypoint : Transform[];
private var pointA : Vector3;
private var currentWaypoint : int;
private var wayDir : int = 1;
function Update ()
{
//if ( currentWaypoint < waypoint.length )
//{
var target: Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;
if ( moveDirection.magnitude < 1 )
{
currentWaypoint += wayDir;
Debug.Log( "currentWaypoint " + currentWaypoint );
if ( currentWaypoint >= waypoint.length )
{
currentWaypoint = waypoint.length - 1;
wayDir = -1;
}
else if ( currentWaypoint < 0 )
{
currentWaypoint = 0;
wayDir = 1;
}
Debug.Log( " : currentWaypoint " + currentWaypoint + " : wayDir " + wayDir );
}
else
{
velocity = moveDirection.normalized * forwardSpeed;
}
rigidbody.velocity = velocity;
//}
}
Hi,
What are you are trying to do is called waypoint looping
I have posted a script for the above here
It is an AI script
read the Update function of my script it contains looping to test it just copy the required code
excluding animation code if you wish.it may seem a bit long but it is a very simple one.The animation code is making it long
okay heres a simple script
var waypoint:Transform[];
var currentwaypoint:int;///Sets the number to infinity
var loop:boolean = false;
var character: CharacterController;
var rotationspeed:float = 1.9;
function Update(){
if(currentwaypoint < waypoint.Length){
var way = waypoint[currentwaypoint].position;///start of detecting waypoint pos
var direction:Vector3 = way - transform.position;
if(direction.magnitude < 1){
currentwaypoint++;
character.Move(direction.normalized * walkspeed * Time.deltaTime);
//////////to rotate to wards the waypoint(optional)///////
transform.eulerAngles = Vector3(0,transform.eulerAngles.y,0);
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(direction),rotationspeed * Time.smoothDeltaTime);
/////////////////////////
}////end of detecting waypoint pos
} else {/////detecting wether to loop or not
if(loop){
currentwaypoint = 0;
}
}///end of detecting loop
}////end of update
I have given a lot of space between the codes to make them easier to understand
Sorry for the late reply