- Home /
Question by
twoface262 · Apr 27, 2012 at 05:03 PM ·
aidistancecheckpoint
Following the next item in an array
I'm trying to make AI that looks for the closest checkpoint to the enemy and the player. Here is what I got so far -
if(checkpoints.Length > 0){
var nearcheck = checkpoints[0];
var distance = Vector3.Distance(transform.position, checkpoints[0].transform.position);
for(var i=0;i<checkpoints.Length;i++) {
var tempdistance = Vector3.Distance(transform.position, checkpoints[i].transform.position);
if(tempdistance < distance ) {
if(tempdistance > 10){
nearcheck = checkpoints[i];
}
}
}
var rotation = Quaternion.LookRotation(nearcheck.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 10);
transform.Translate(Vector3.forward * 10 *Time.deltaTime);
But I'm stuck... How would I make it calculate the distance between the closest checkpoint to the ai and the player? And then follow the checkpoints in a series. Right now it's just spinning around the closest checkpoint to the AI. Thanks!
Comment
Your answer
Follow this Question
Related Questions
Distance thing with gameobject ? 4 Answers
Speed based on Distance 3 Answers
A.I - what is better for player checking ? 0 Answers
A* pathfinding - how to put penalty below a character 0 Answers
Distance between transform.position.x & target.position.x 2 Answers