Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 devstudents · Feb 15, 2017 at 06:16 AM · c#pathfindinggrid

Stuck with A* Pathfinding Script

I've posted this question on Stack Exchange, but I'm not getting much help. Hopefully there's a pathfinding guru somewhere on this forum who can help me out! The question:

I'm using Sebastian Lagues' pathfinding system found in these tutorials: https://www.youtube.com/playlist?list=PLFt_AvWsXl0cq5Umv3pMC9SPnKjfp9eGW.

I've started extending his code for a dynamic RTS type game, but I've run into a problem. The units do not land on the target node, but one node before. This is problematic as for my game I need the units to land directly on the target node.

I'm usually happy to solve problems alone, but I don't even know where to begin fixing this. The project is found here (episode 5): https://github.com/SebLague/Pathfinding

Thanks in advance for any help or suggestions.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by HenryStrattonFW · Feb 16, 2017 at 07:53 PM

Success! ok so I had a look over it, had to take a few parts out one by one to work out where the issue was coming from, I've found it!.

In the Simplify path method you start your for loop from index 1 (so that you can always do i-1 to i for direction). However because of this, path[0] never makes it through the simplify stage, and always get culled out of the list. So all i did was update the Simplify method add path[0] to the waypoints list just before the for loop starts up.

(Note I did still have the previous addition mentioned in my answer, I'm unsure why this causes you issues, but in any case it does not appear to make a difference to the final target location).

Comment
Add comment · Show 4 · 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
avatar image devstudents · Feb 18, 2017 at 09:04 AM 0
Share

Thanks a lot! For the last couple of days I've been using some hacky code that moved the unit to the target node when they reached the end of the path. I really wanted that to happen by default, so I really appreciate the help. I'm using this pathfinding code as part a Simple RTS series I'm doing on youtube. If you're interested, I'd appreciate any critical feedback. You can find the tute and a link to the project here: https://www.youtube.com/watch?v=OqADgd05fpQ

avatar image HenryStrattonFW devstudents · Feb 18, 2017 at 08:13 PM 1
Share

Happy to help. Gave the video a quick look and it looks good, could be a very useful series for some people. A few things that you do differently to how I would do them. For example, you have a Unit class, which to me would be a good place to store a reference to the selection circle, and then expose a method on the unit like "SetSelected(bool)" for turning it on/off, to me this way feels more flexible, and cleaner, but that could just be more down to personal preference as the way you are doing it is perfectly functional

That being said, you asked for critical feedback and there was one thing that you commented on a few times in the video regarding how Get$$anonymous$$ouseButtonDown worked, and how you could not put for loops inside that statement because it is only present for one frame. I'll be blunt, what I understood you to be saying in that regard is completely wrong. You could put as much logic as you wanted in that statement and it will all be processed, because the next frame of the game will not occur until that code completes. Granted I would advise not putting anything that is hugely computationally expensive in there to avoid lag spikes (perhaps this is what you meant, if so feel free to ignore this whole section), but for the purposes of iterating over a list to add/remove units, unless dealing with a huge number of units, or insanely costly actions as part of your deselection/selection logic I see no reason that you could not put this logic within that if statements scope.

avatar image devstudents HenryStrattonFW · Feb 19, 2017 at 03:31 AM 0
Share

Thanks a lot for the feedback! I should express those statements more tentatively in future. I guess I just assumed that's how it worked as when I put the for loop that ran through all the units and deselected them in the getmousebutton down if statement, only some were deselected. I had to click the ground a couple of times for them all to be deselected.

There must be another cause for this behaviour, although I'm not sure what it is. I should probably start another forum post about it. I'll put a link to the question here if you ever get a chance to look into it. Thanks again for solving this issue and for the helpful feedback :)

Show more comments
avatar image
1

Answer by HenryStrattonFW · Feb 15, 2017 at 10:25 PM

I've not downloaded the project to try, but had quick look at the source on github and there's one thing that jumps out at me as a likely cause, your retrace function.

 Vector3[] RetracePath(Node startNode, Node endNode) {
         List<Node> path = new List<Node>();
         Node currentNode = endNode;
         
         while (currentNode != startNode) {
             path.Add(currentNode);
             currentNode = currentNode.parent;
         }
         Vector3[] waypoints = SimplifyPath(path);
         Array.Reverse(waypoints);
         return waypoints;
         
     }

Notice that once currentNode == startNode you break out of the while loop, but in doing so do not then add the currentNode to the path, meaning that "startNode" never makes it into the list. so once reversed you would be 1 node short.

Try adding a path.Add(CurrentNode);after your while loop before you simplify your path, and see if that sorts it out, unless that node is being added to the list elsewhere and I missed it, this should solve the problem.

Comment
Add comment · Show 2 · 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
avatar image devstudents · Feb 16, 2017 at 01:52 AM 0
Share

Thanks for the reply! Unfortunately, adding the currentNode there creates an index out of range exception when the character reaches the end of the path.

avatar image HenryStrattonFW devstudents · Feb 16, 2017 at 07:30 PM 0
Share

downloaded now, oddly enough I do not get a null ref when I add the start node, however I also do not seem to get the units to sit on top of the target node either, they still seem to stop one early.

I'll have a deeper look into it and let you know if I work it out.

avatar image
0

Answer by J0hn4n · Feb 20, 2017 at 10:25 AM

just put -1 on the path index :3

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

286 People are following this question.

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

Related Questions

Distribute terrain in zones 3 Answers

RTS Grid Initialisation 0 Answers

A* Pathfinding Project Problem with Obstacles 2 Answers

Astar Pathfinding not scanning graph 0 Answers

Multiple Cars not working 1 Answer


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