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
1
Question by tgnass · Oct 02, 2013 at 12:02 PM · pathfindingastar

How to find the shortest path?

So I have a unit on the screen (the one with a green box around it) and lets say I want to move to the target star (circle in red).

I know how to make the unit move to that star in a straight line, but how would I make it find the shortest path to the star (bottom picture)?

All the stars randomly generate when the scene starts.

I've been looking at A* but couldn't figure out a way to get this working the way I want too.

I just don't know how to start with this.

A little help please?

Star

Target star

Path to star

Thank you!

target star.png (9.8 kB)
path to star.png (10.2 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
7

Answer by CHPedersen · Oct 02, 2013 at 12:32 PM

First off, great question, and great with images to give examples. :)

This is a very typical use case for Dijkstra's Shortest Path algorithm. Read the wiki article - it has code examples too. It finds the shortest path in a graph, which is what you've got here, if you imagine that the stars are connected by edges (the yellow paths in the final image).

This kind of algorithm would also typically be used to find the shortest path between cities in travel planning (with the roads as edges in the graph).

Bonus: There are variants of this algorithm that allow you to assign weights to the edges, so some become more favourable than others. These algorithms then select the lowest edge weight sum.

Comment
Add comment · Show 6 · 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 tgnass · Oct 02, 2013 at 02:11 PM 0
Share

Alright thanks, I'll look into it.

avatar image Owen-Reynolds · Oct 02, 2013 at 02:27 PM 1
Share

But Dijkstra's and A* are pretty much the same thing. They both use open/closed sets to grow a blob of places you know how to get to. A* just complicates it with the f,g,h functions, to make it hopefully reach the target faster (Dijkstra's blindly grows until it accidentally reaches the target.)

Also, textbook Dijkstra's does assume there are edge weights. (you look for the shortest edge from your blob to a new spot.)

avatar image tgnass · Oct 02, 2013 at 02:35 PM 0
Share

So what one should I go for?

avatar image CHPedersen · Oct 03, 2013 at 10:25 AM 0
Share

@Owen Reynolds: Thanks for the clarification. :) I wasn't aware A* had so much in common with Dijkstra's.

@tgnass: I think your choice to begin with should come down to a performance consideration. $$anonymous$$y immediate thought in that regard is that, judging by your screenshots, it doesn't look like you're going to have that many stars in the graph. So, I don't think there is terribly much to be gained from choosing a more complicated scheme for performance reasons. Thus, the choice ultimately becomes a matter of convenience. I would go with whatever is simpler to implement, and only upgrade to something faster if its performance becomes an issue later.

avatar image tgnass · Oct 05, 2013 at 12:44 PM 0
Share

Thank you for the help, I'll have to look into both and see which one would work for me.

Show more comments

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

17 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

Related Questions

How to calculate distance based on A*? 2 Answers

Path finding algorithm for 2d game 2 Answers

How do i get the IsPathPossible() function to ignore some nodes using Astar Pathfinding Project 0 Answers

Implementation of Navmesh terrain dijkstra algorithm 0 Answers

Pathfinding unity 3 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