- Home /
waypoint ai script not going all the way to waypoint
hello, everyone. I have this waypoint ai script, but it doesn't go all the way to the waypoints, it stops short I have no idea how to fix this. all help would be appreciated.
Here is my code:
@script RequireComponent(Rigidbody) enum mt { Sequence, Patrol } var waypoint : Transform[]; var speed : float = 20; var movementType : mt; private var currentWaypoint : int; function Awake() { if(movementType == mt.Patrol) transform.position = waypoint[0].position; } 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(movementType == mt.Patrol)
currentWaypoint = 0;
}
rigidbody.velocity = velocity;
}
How close to the waypoint does it go? From a brief scan of your code, I would say that it either stops 1 unit away from the waypoint (because you check for if(moveDirection.magnitude < 1) and not(moveDirection.magnitude < 0.1) for example, or it overshoots it because you're using the velocity of the rigidbody to move, and expecting the physics engine to stop it in time. Is your behaviour similar to what I'm describing? Or does it stop REALLY away from the next waypoint?
Answer by Henrique Vilela · May 05, 2011 at 01:36 PM
Looks like you're changing to the next waypoint too early. If you're using the right scale, 1 means 1 meter so try to use a smaller number like 0.1 (10 centimeters).
if (moveDirection.magnitude < 0.1) {
// waipoint reached
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Clearing a destination... 1 Answer
create shortest AI path from/to object. 1 Answer
Confused about copying Lists! 1 Answer
AI Waypoint help or suggestions!? 2 Answers