Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Salmjak · Jul 13, 2015 at 05:02 PM · c#listpathfindinglinqnodes

Custom pathfinding and node-values

Hi! I'm working on a 2D platformer with a custom physics system. I use a grid-based layout for my map (procedural generation) and store all values in a two-dimensional integer (int[,]).

I've started on an AI, and I got walking, climbing and jumping already (if the AI walks on a linear path along teh x-axis he will know how to climb and jump to get to the destination on the x-axis).

So I've started on pathfinding. I started by copying my values from my int[,] and depending on if it is a solid or empty block I define different penalties.

I loop through all coordinates and assign a score based on this formula:

 float score = penalty[i,u] + (new Vector3(i,u,0) - destination).sqrMagnitude + (new Vector3(i,u,0) - transform.position).sqrMagnitude;

The problem here is that when I order the List of nodes based on their score (nodes = nodes.OrderBy(x => x.score).ToList();), the node between my starting position and destination will always have the lowest score, and the nodes next to this node will have second to lowest, etc. So I end up with my AI going back and forth, never really reaching his destination.

How can I improve this formula or sorting of the list, so that the nodes will come in order depending on score and how close they are to transform.position? Is it possible to sort with two variables in Linq?

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
0

Answer by Salmjak · Jul 13, 2015 at 06:30 PM

Kind of solved this by doing more of an A* approach, checking neighbours of a tile and their distance to the destination (+ penalty) and then choosing the cheapest neighbour. Add that neighbour to a list of nodes, and check it's neighbours. Then I loop this until the node-position is at the destination.

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

21 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

Related Questions

A node in a childnode? 1 Answer

Pathfinding through pairs of connections 2 Answers

C#, LINQ and Lists - help with 1 line of code? 1 Answer

A* pathfinding - incorrect nodes calculated 1 Answer

Sort List by string field 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