Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Raspilicious · Jun 04, 2012 at 07:25 AM · pathfindinggridhexagon

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 :)

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image phren · Jun 04, 2012 at 08:47 AM 0
Share

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.

avatar image Raspilicious · Jun 06, 2012 at 10:27 AM 0
Share

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. :)

avatar image pcjacob · Sep 26, 2012 at 03:38 AM 0
Share

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.

avatar image Summoner · Feb 06, 2013 at 07:55 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges