- Home /
Confusion with Aron Granberg's A* Pathfinding
Hello people :)
I'm currently working on a turn-based project which requires pathfinding based on a hexagon grid, and have been looking into Aron Granberg's A* Pathfinding packages (which are extremely powerful from what I've seen so far).
But I've come up against a snag... I can't work out exactly how to make the unit (or game object, that is) move exactly along the path, consisting of a series of waypoints or 'nodes,' each in the centre of each hexagon which make up the grid.
What happens at the moment is that the unit travels in a straight line toward a waypoint, but it has a radius around it which searches for the waypoint to confirm the 'waypoint reached.' When this radius (say a radius of 1) touches the waypoint, the unit instantly starts moving toward the next waypoint, effectively cutting the corner of the turn. Scale-wise, think of the unit itself being say 0.2 radius, and the detection radius being about 1. This may seem ok in normal navigation, but it doesn't suit the hex-grid-based pathfinding I'm trying to accomplish.
Here's the code I have came up with so far...
using UnityEngine;
using System.Collections;
using Pathfinding;
public class AstarAI : MonoBehaviour {
public Vector3 targetPosition;
public GameObject targetNode;
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 = 1;
//the waypoint we are currently moving towards
private int currentWaypoint = 0;
public GameObject clickedObject;
public Ray ray;
public RaycastHit hit;
public void Start () {
controller = GetComponent<CharacterController>();
}
public void MyCompleteFunction (Path p) {
Debug.Log ("Yay we got a path back! Is there an error?" + p.error);
if (!p.error) {
path = p;
//reset the waypoint counter
currentWaypoint = 0;
}
}
public void Update () {
//Find the closest node to this GameObject's position
Node node = AstarPath.active.GetNearest (transform.position);
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100)) {
targetPosition = AstarPath.active.GetNearest (hit.point);
Debug.DrawLine(ray.origin, hit.point);
if (node.walkable) {
Debug.Log ("this is walkable" + node.position);
MoveOnThePath();
}
}
if (path == null) {
//we have no path to move toward yet
return;
}
if (currentWaypoint >= path.vectorPath.Length) {
Debug.Log ("End of path reached");
return;
}
//direction to the next waypoint
Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;
dir *= speed * Time.deltaTime;
controller.SimpleMove (dir);
if (Vector3.Distance (transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
currentWaypoint++;
return;
}
}
public void MoveOnThePath () {
Seeker seeker = GetComponent<Seeker>();
controller = GetComponent<CharacterController>();
seeker.StartPath (transform.position, targetPosition, MyCompleteFunction);
}
}
Any help would be greatly appreciated!
Thanks, Aaron :)
This might be a stupid question, but have you just tried setting nextWaypointDistance to for example 0.1? In this case it should be enough for the character to start arriving closer to the center of the hex. You should probably just try to find the lowest possible value for nextWaypointDistance that still works.
I tried that, but for some reason if I set it to zero, or anything under say 0.2, then it didn't move at all. I've tweaked it over the past couple days, now it works.
I added a boolean that became true when the radius was less than 1, which enabled a countdown timer (run by Update()) to count down. When it became equal to or less than zero, then i activated currentWaypoint++ and reset the countdown timer to its original value of 0.4 (sec.).
This seems to work rather well. The unit moves from its hex all the way to the centre of the next hex (the next waypoint, that is), gradually slowing down (over 0.4 seconds) as it nears the waypoint. When it reaches the actual waypoint/centre of hex, the timer reaches 0, and the unit starts moving toward its next waypoint.
It'll do for now, but eventually I'll want to remove the gradual speed decline, to have it move at a constant speed all the way to the waypoint... but that is for another question.
If anybody wants the code I'll post it up. :)
I also have a question about Granberg's pathfinding, So I thought I might ask here ins$$anonymous$$d of creating a new question.
I am trying to make a make a really simple game mechanic : Looking from down top, you click on a position and the character (my capsule in this case) navigates to this position.
I have followed Granberg's guide (http://www.arongranberg.com/astar/docs/getstarted.php) as much as I could, then implemented the position detection on click. As you can see in the guide, the green gizmo line is always parallel to the floor, from start to finish. http://www.arongranberg.com/astar/docs/images/getstarted/getStarted_step3.png
Now if you look at my cylinder and gizmo, the green line starts from the center of my cylinder, dips to the floor and then finally goes up to the given point of origin, which you can see here along with my a* configuration and my code. http://i.imgur.com/kocUr.jpg
I can show all my code but I did not add much more. As you can see, this causes a real problem. The cylinder only makes about 10% of the path then stops to a dead end. $$anonymous$$aybe I am having a bigger version of the problem Aaron is having ?
Thank you for the help.
Could you post you code Raspilicious? I am currently trying to solve exactly what you were talking about, and am curious how what you did, differed from what I did, since my version didn't work.
Answer by CorbieMedia · Apr 14, 2015 at 03:46 PM
I actually had this problem too: if the radius to check for a reached waypoint was too small (my case was somewhere around 1.8), the seeker wouldn't move at all. I was losing my mind over it for a couple of days, until I noticed that the seeker object's transform.position.z was 1.8. It was quite easy to overlook this since it didn't affect anything else and it wasn't visible in the 2D viewport.
Your answer
Follow this Question
Related Questions
Astar Pathfinding not scanning graph 0 Answers
Tiles Slow Down Game 0 Answers
Highlighting tiles on a hexagonal grid 1 Answer
Resize hex grid,Need to resize hex grid 0 Answers
Unity Probuilder mesh brush and grids. 0 Answers