- Home /
AI Waypoint help or suggestions!?
i wanna know how to make a script that has two GameObject variables, waypoint1 and waypoint2, which then tells the object the script will be attached to, to walk back and forth in between the waypoints.please help! all help is Greatly appreciated!!
Answer by Mike 3 · Aug 25, 2010 at 01:03 PM
Take a look at the Spline Controller script on the wiki - you can set up two (or possibly more, so that turning looks better at the ends) control points and have your object walk between them
http://www.unifycommunity.com/wiki/index.php?title=Spline_Controller
Preferably download the c# version, as it has less bugs than the js version (and is 3.0 compatible if I remember correctly) - and looking at the site again, maybe even the iPhone compatible version, as it's got another fix in
Answer by oinkoinkflapflap · Dec 04, 2010 at 01:23 PM
i got most of the following from a great youtube vid, but here are the scripts and instructions and stuff
make an empty game object, call it "waypoint" or whatevers easy and attatch this 2 it you can see it on creating but not on game which is handy :).
function OnDrawGizmos(){
Gizmos.DrawIcon(transform.position,"AITuts/Materials/Textures/WayPoint");
}
ok then put this were u want it 2 move. put the following on what you want to go there (your AI)
var waypoint : Transform[];
var speed : float = 20;
private var currentWaypoint : int;
var loop : boolean = true;
function Awake(){
waypoint[0] = transform;
} 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 * speed;
}
} else{ if(loop){ currentWaypoint=0; } else{ velocity = Vector3.zero; } }
rigidbody.velocity = velocity;
}
then add a "rigidbody" to your AI (it's best 2 use a sphere or a capsule for were you attach your script)
then on the object click the name "waypoint" and change the "size" number to how ever many waypoints you want. with the waypoints i explained earlier drag them into the empty slots. when it gets to the end it should do a loop, if it doesn't make the size 1 bigger and but the 1 before the last to the end, if this is a bit confusing i recommend the youtube vid (link at the end) there are 3 vids in the series that will help with what i explained if you want it to point and shoot, say and i can help with that 2 :)
http://www.youtube.com/user/steamisM50#p/a/DC891A19DE6D2322/0/POUbAsr-6q4
this guy helps.........
there r bits of scripts here that arn't in colours, so dont forget them! ;)