Search Unity

A* pathfinding: Object stopping short

Discussion in 'Scripting' started by jamstorr86, Sep 19, 2014.

  1. jamstorr86

    jamstorr86

    Joined:
    Sep 5, 2014
    Posts:
    8
    I have got a weird problem where it appears that the End of Path reached is occurring too early, and so my gameobject stop short of the target position.

    See screen shot

    upload_2014-9-19_8-32-55.png

    I have tried many different things to solve this, such as adjusting the Seeker settings, the grid settings etc.

    I am currently using a Grid Graph.

    Also, please see my script for moving the sphere below. I have used the most simplest script I could to try and narrow down the issue.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using Pathfinding;
    4.  
    5. public class V3_enemy_Move : MonoBehaviour {
    6.  
    7.         //The point to move to
    8.         public Vector3 targetPosition;
    9.        
    10.         private Seeker seeker;
    11.         private CharacterController controller;
    12.        
    13.         //The calculated path
    14.         public Path path;
    15.        
    16.         //The AI's speed per second
    17.         public float speed = 100;
    18.        
    19.         //The max distance from the AI to a waypoint for it to continue to the next waypoint
    20.         public float nextWaypointDistance = 3;
    21.        
    22.         //The waypoint we are currently moving towards
    23.         private int currentWaypoint = 0;
    24.        
    25.         public void Start () {
    26.             targetPosition = new Vector3 (0, 0, 0);
    27.             seeker = GetComponent<Seeker>();
    28.             controller = GetComponent<CharacterController>();
    29.            
    30.             //Start a new path to the targetPosition, return the result to the OnPathComplete function
    31.             seeker.StartPath (transform.position,targetPosition, OnPathComplete);
    32.         }
    33.        
    34.         public void OnPathComplete (Path p) {
    35.             Debug.Log ("Yay, we got a path back. Did it have an error? "+p.error);
    36.             if (!p.error) {
    37.                 path = p;
    38.                 //Reset the waypoint counter
    39.                 currentWaypoint = 0;
    40.             }
    41.         }
    42.        
    43.         public void FixedUpdate () {
    44.             if (path == null) {
    45.                 //We have no path to move after yet
    46.                 return;
    47.             }
    48.            
    49.             if (currentWaypoint >= path.vectorPath.Count) {
    50.                 Debug.Log ("End Of Path Reached");
    51.                 return;
    52.             }
    53.            
    54.             //Direction to the next waypoint
    55.             Vector3 dir = (path.vectorPath[currentWaypoint]-transform.position).normalized;
    56.             dir *= speed * Time.fixedDeltaTime;
    57.             controller.SimpleMove (dir);
    58.            
    59.             //Check if we are close enough to the next waypoint
    60.             //If we are, proceed to follow the next waypoint
    61.             if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
    62.                 currentWaypoint++;
    63.                 return;
    64.             }
    65.         }
    66.     }
    67.  
    anyone have any ideas?

    James