enter code here
var Waypoint : Transform[];
var loop : boolean = true;
var speed : float = 20;
private var currentWaypoint : int;
private var timer = 0;
function Awake()
{
Waypoint[0] = transform;
}
function Update()
{
if(currentWaypoint < Waypoint.length)
{
var target : Vector3 = Waypoint[currentWaypoint].position;
var move_direction : Vector3 = target - transform.position;
var velocity = rigidbody.velocity;if(move_direction.magnitude < 1)
{
currentWaypoint++;
}else{
velocity = move_direction.normalized * speed;
}
}
else{
if(loop)
{
currentWaypoint = 0;
}else{
velocity = Vector3.zero;
}
}
rigidbody.velocity = velocity;
}
How can i add timer to it? Like this. AI will go through all waypoints and then stops for 5 seconds and then go through those waypoints again and then again stops for 5 seconds and do it again in loop.
Hope this helps, put the number of way points you want to use in the inspector then drag your waypoints in to the inspector.
#pragma strict
var waypoint : Transform[]; // The amount of Waypoint you want
var patrolSpeed : float = 3; // The walking speed between Waypoints
var loop : boolean = true; // Do you want to keep repeating the Waypoints
var player : Transform; // Referance to the Player
var dampingLook = 6.0; // How slowly to turn
var pauseDuration : float = 0; // How long to pause at a Waypoint
private var curTime : float;
private var currentWaypoint : int = 0;
private var character : CharacterController;
function Awake(){
}
function Start(){
character = GetComponent(CharacterController);
}
function Update(){
if(currentWaypoint < waypoint.length){
patrol();
}else{
if(loop){
currentWaypoint=0;
}
}
}
function patrol(){
var target : Vector3 = waypoint[currentWaypoint].position;
target.y = transform.position.y; // Keep waypoint at character's height
var moveDirection : Vector3 = target - transform.position;
if(moveDirection.magnitude < 0.5){ // If this number is 1 the character will jerk the last bit to the waypoint and not be over it
// any lower and the character will get stuck for a second over the waypoint on the iPhone
if (curTime == 0)
curTime = Time.time; // Pause over the Waypoint
if ((Time.time - curTime) >= pauseDuration){
currentWaypoint++;
curTime = 0;
}
}else{
//transform.LookAt(target); // Use this instead of below to look at target without damping the rotation
// Look at and dampen the rotation
var rotation = Quaternion.LookRotation(target - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
}
}