- Home /
AI will complete patrolling and wait for 5 seconds
Hi!
I have a question. I have this script:
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.
Answer by Griffo · Aug 08, 2012 at 12:17 PM
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);
}
}
Nope. That doesnt work. when i hit play that cube just stands there and isnt doing anything. here is my inspector: http://imageshack.us/photo/my-images/94/patrolinspector.png/
Sorry forgot to say add a character controller to the cube, replace the box collider ...
Sorry, still nothing and i replaced box collider with character controller. but when i hit play nothing happens.
Have a look at my pic in the answer further up, I've just made a new scene with a cube with a character controller and 3 empty game objects for my waypoints and dragged them into the inspector and it works fine .. with the script dragged onto the cube ..
I've added the .js file to the above answer ..
Answer by Griffo · Aug 08, 2012 at 01:06 PM
Sorry forgot to say add a character controller to the cube, replace the box collider ...
Your answer
Follow this Question
Related Questions
free roaming enemy ai 1 Answer
iTween Help Please MoveTo not working 1 Answer
how to detect if transform.lookat is going through walls? 1 Answer
Send an enemy back to its spawn point using waypoints 2 Answers
AI patrolling script 1 Answer