I’m attempting to have my character move based on a waypoint: from an overhead position, the player clicks on the ground to create a way point. The camera then moves to first person perspective where the player moves via pathfinding.
Essentially, the player’s current position is the path start and the end is the waypoint. The issue: the full path is not being drawn to the waypoint. The path length may only be 1 or 2 units (the console log might say 0 nodes searched or 2 nodes searched) when the actual full path might need to be 50 units. If I set a few waypoints that move the player 1 or 2 nodes, it typically then gets “on track” where it finds the full path and moves accordingly. Then each waypoint set after that seems to work fine. It almost seems that the player is “off track” and needs to get on track before the full path can be drawn.
There is also a strange behavior when the character is moving along the path that is not fully drawn in that that the player transform (currently a capsule) is pushed up the y axis about 0.8 units until it finds the full path (after setting a couple different waypaths) where it drops to y = 0.3 then moves along the path correctly. It is like it is climbing over something, but there is nothing there. It doesn’t matter if I start the player off in different position or not.
The code is spread throughout the script, but I believe these are the salient elements.
I currently have this in Update() as a workaround to keep redrawing the path until the full path is found. It doesn’t always work and when it does, it’s choppy due to the strange vertical movement discussed above…
if (Vector3.Distance(player.position,waypoint1)>.5 && waypointSet == "yes" && lastCount <= 2){
CreatePath();
lastCount = path.vectorPath.Count;
}
Checking currentWaypoint…
if (currentWaypoint >= path.vectorPath.Count){
Debug.Log("End of Path Reached");
}
Movement…
player.position = Vector3.MoveTowards(player.position, path.vectorPath[currentWaypoint],Time.deltaTime * speed);
if ((Vector3.Distance(player.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance) && (currentWaypoint+1 < path.vectorPath.Count))
{
currentWaypoint++;
}
functions…
function CreatePath(){
waypoint1 = Vector3(adjustedPoint.x,0,adjustedPoint.z);
Debug.Log(waypoint1);
seeker.StartPath(player.position, waypoint1, OnPathComplete);
Debug.Log("Count: " + path.vectorPath.Count);
waypointSet = "yes";
}
public function OnPathComplete (p : Pathfinding.Path) {
Debug.Log ("Yey, we got a path back. Did it have an error? "+p.error);
if (!p.error) {
path = p;
currentWaypoint = 0;
}
}