Well, im having some problem to update the path with dynamic grid.
I’m updating the grid while my monsters move, but im having some problem with the path for my robot.
The path is not update while the grid is.
Look this video to understand better what im saying: https://www.youtube.com/watch?v=AAnQ05lSoPU&feature=youtu.be
The code im using for my robot is that:
`
using UnityEngine;
using System.Collections;
using Pathfinding;
public class AstarAI : MonoBehaviour {
public Transform target;
public Vector3 targetPosition;
private Seeker seeker;
private CharacterController controller;
//The calculated path
public Path path;
//The AI's speed per second
public float speed = 100;
//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;
float gravity = 20.0F;
public void Start() {
targetPosition = target.transform.position;
//Get a reference to the Seeker component we added earlier
seeker = GetComponent<Seeker>();
controller = GetComponent<CharacterController>();
//Start a new path to the targetPosition, return the result to the OnPathComplete function
seeker.StartPath(transform.position, targetPosition, OnPathComplete);
}
public void OnPathComplete(Path p) {
//Debug.Log ("Yey, we got a path back. Did it have an error? "+p.error);
if (!p.error) {
path = p;
//Reset the waypoint counter
currentWaypoint = 0;
}
}
public void FixedUpdate() {
if (path == null) {
//We have no path to move after yet
return;
}
if (currentWaypoint >= path.vectorPath.Count) {
Debug.Log("End Of Path Reached");
return;
}
//Direction to the next waypoint
Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
dir *= speed * Time.fixedDeltaTime;
dir.y -= gravity * Time.fixedDeltaTime;
controller.Move(dir);
//Check if we are close enough to the next waypoint
//If we are, proceed to follow the next waypoint
if (Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
currentWaypoint++;
return;
}
}
}
`
I tryed to put: //Start a new path to the targetPosition, return the result to the OnPathComplete function seeker.StartPath(transform.position, targetPosition, OnPathComplete);
inside the update function, but it not worked.
What should i do?
Thanks
The path will not update by itself, you need to request a new path when you want it to update. However putting the request in the Update function is not good either, because the system uses time slicing, it will rarely complete in the exact same frame as it was requested, so it will progress to the next frame and at that point a new path request is made which will cancel the previous one (the seeker assumes that you only want the latest results). So what you should do is to request an update only if seeker.IsDone() evaluates to true.
Also. The way you are updating the graph makes a large region around the enemies unwalkable. I am not sure if that is intended, but you really shouldn’t do that. Since the enemies use pathfinding as well, they will basically think that they are standing in the middle of a wall and will try to get out of it as quickly as possible. From the video it looks like you are using a sphere collider as a trigger for the enemies. You might want to uncheck Unity Physics Settings -> Raycast Hits Triggers.
well, i tryed to use this function “seeker.IsDone()”, but its not working well, as u can see in this new video the enemy go for a while then go back.
`using UnityEngine;
using System.Collections;
using Pathfinding;
public class AstarAI : MonoBehaviour {
public Transform target;
public Vector3 targetPosition;
private Seeker seeker;
private CharacterController controller;
//The calculated path
public Path path;
//The AI's speed per second
public float speed = 100;
//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;
float gravity = 20.0F;
public void Start() {
targetPosition = target.transform.position;
//Get a reference to the Seeker component we added earlier
seeker = GetComponent<Seeker>();
controller = GetComponent<CharacterController>();
seeker.StartPath(transform.position, targetPosition, OnPathComplete);
}
void Update() {
if (seeker.IsDone()) {
//Start a new path to the targetPosition, return the result to the OnPathComplete function
seeker.StartPath(transform.position, targetPosition, OnPathComplete);
}
if (path == null) {
//We have no path to move after yet
return;
}
if (currentWaypoint >= path.vectorPath.Count) {
Debug.Log("End Of Path Reached");
return;
}
//Direction to the next waypoint
Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
dir *= speed * Time.fixedDeltaTime;
dir.y -= gravity * Time.fixedDeltaTime;
controller.Move(dir);
//Check if we are close enough to the next waypoint
//If we are, proceed to follow the next waypoint
if (Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
currentWaypoint++;
return;
}
}
public void OnPathComplete(Path p) {
//Debug.Log ("Yey, we got a path back. Did it have an error? "+p.error);
if (!p.error) {
path = p;
//Reset the waypoint counter
currentWaypoint = 0;
}
}
public void FixedUpdate() {
}