x


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.

more ▼

asked Aug 08 '12 at 12:11 PM

subzul gravatar image

subzul
16 2 5 8

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Sorry forgot to say add a character controller to the cube, replace the box collider ...

link text

inspector.jpg (35.9 kB)
more ▼

answered Aug 08 '12 at 01:06 PM

Griffo gravatar image

Griffo
1.7k 20 48 66

(comments are locked)
10|3000 characters needed characters left

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);
 } 
}
more ▼

answered Aug 08 '12 at 12:17 PM

Griffo gravatar image

Griffo
1.7k 20 48 66

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/

Aug 08 '12 at 12:48 PM subzul

Sorry forgot to say add a character controller to the cube, replace the box collider ...

Aug 08 '12 at 12:57 PM Griffo

Sorry, still nothing and i replaced box collider with character controller. but when i hit play nothing happens.

Aug 08 '12 at 01:04 PM subzul

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 ..

Aug 08 '12 at 01:08 PM Griffo

Thanks a lot. It works perfectly now.

Aug 08 '12 at 01:23 PM subzul
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x956
x170
x12

asked: Aug 08 '12 at 12:11 PM

Seen: 489 times

Last Updated: Aug 08 '12 at 01:24 PM