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?
– anon23753345