I’ve got a problem with my seekers becoming extremely jerky/laggy as they near their target.
Example video: https://www.youtube.com/watch?v=XcPd2iuZrW0&feature=youtu.be
Note how the intended smooth movement plays out early in the run, but then it quickly just begins stuttering/lagging.
Inspector for the seeker GameObject: https://i.imgur.com/vrKi5Wk.png
Code for the seeker (inside the Soldier.cs file):
private CharacterController controller;
private Seeker seeker;
private Path path;
private Vector3 targetPosition;
public float speed = 0.1f;
// The max distance from the AI to a waypoint for it to continue to the next waypoint
public float nextWaypointDistance = 3;
// The waypoint we are currently moving towards
private int currentWaypoint = 0;
// How often to recalculate the path (in seconds)
public float repathRate = 0.5f;
private float lastRepath = -9999;
// Use this for initialization
void Start () {
seeker = GetComponent<Seeker>();
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
public void Update()
{
if (Time.time - lastRepath > repathRate && seeker.IsDone())
{
lastRepath = Time.time + Random.value * repathRate * 0.5f;
// Start a new path to the targetPosition, call the the OnPathComplete function
// when the path has been calculated (which may take a few frames depending on the complexity)
seeker.StartPath(transform.position, targetPosition, OnPathComplete);
}
if (path == null)
{
// We have no path to follow yet, so don't do anything
return;
}
if (currentWaypoint > path.vectorPath.Count) { return; }
if (currentWaypoint == path.vectorPath.Count)
{
Debug.Log("End Of Path Reached");
currentWaypoint++;
return;
}
// Direction to the next waypoint
Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
dir *= speed * Time.deltaTime; //yes I've tried it without the Time.deltaTime multiplier too.
// Note that SimpleMove takes a velocity in meters/second, so we should not multiply by Time.deltaTime
controller.Move(dir);
Debug.Log("Moving in dir: " + dir);
// The commented line is equivalent to the one below, but the one that is used
// is slightly faster since it does not have to calculate a square root
//if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
if ((transform.position - path.vectorPath[currentWaypoint]).sqrMagnitude < nextWaypointDistance * nextWaypointDistance)
{
currentWaypoint++;
return;
}
}
Could anyone shed some light on what might be wrong here? I followed the fairly basic tutorial outlined in the startup guide, but I’m having no joy with it.