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 SerialXym · Sep 20, 2015 at 07:20 PM · pathfindingdijkstra

How do I highlight all available paths with Dijkstra's algorithm on a square tile-based map?

I asked this question before, I accepted an answer, because it worked with the variables I tested with, didn't check if it worked when you change them.

Anyway, when I click on a character I want to see how far it can move on a square tile based map, with x amount of movement points that it has got (like Fire Emblem games). 1 movement point allows you to move one tile to the right, left, top or bottom.

Here is my ValidMoves class which should be finding all paths, but I believe it has problems with the movePoints argument and nextMoveCost in the GetValidMoves function

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ValidMoves : MonoBehaviour 
 {
     public List<Node> validMoves = new List<Node>();
     void Start()
     {
         validMoves.Clear();
         //Tiles initialized and startTile is the current Tile
     }
     public void GetValidMoves(Node startTile, int movePoints)
     {
         validMoves.Add(startTile);
         for (int i = 0; i < startTile.neighbours.Count; i++)
         {        
 
             if(!GameManager.instance.tileArray[startTile.neighbours[i].x, startTile.neighbours[i].y].GetComponent<ClickableTile>().isWalkable)
                 continue;
             int nextMoveCost = movePoints - (int) GameManager.instance.tileArray[startTile.neighbours[i].x, startTile.neighbours[i].y].GetComponent<ClickableTile>().movementCost; 
                 if (nextMoveCost >= 0 && !validMoves.Contains(startTile.neighbours[i]))
                     GetValidMoves(startTile.neighbours[i], nextMoveCost);
         }
     }
     public void clearMoves()
     {
         
         validMoves.Clear();
     }
 }
 

validMoves is a list of my class Node, which contains information about each tile, such as it's position, and a list of the class Node called neighbours (the tiles that are connected to it).

 using UnityEngine;
 using System.Collections.Generic;
 
 public class Node 
 {
     public List<Node> neighbours;
     public int x;
     public int y;
     
     public Node() 
     {
         neighbours = new List<Node>();
     }
     
     public float DistanceTo(Node n) 
     {
         if(n == null) {
             Debug.LogError("WTF?");
         }
         
         return Vector2.Distance(
             new Vector2(x, y),
             new Vector2(n.x, n.y)
             );
     }
     
 }
 

GameManager.instance.tileArray is an array of all tiles (GameObject), it's scripts contain the movement cost of the tile (the amount of movePoints needed to walk onto it) and other info. Eventually I will make class Node accessible through the tileArray, but I just haven't gotten around to doing so yet.

The function only works well when the character has either 1 or 2 movement point. When it has more than that, you can see similar patterns and you can notice that not all tiles are being accessed.

Here are some examples:

2 movement points: http://i.imgur.com/o4XrqgI.jpg

3 movement points: http://i.imgur.com/MGX57Qd.jpg

6 movement points: http://i.imgur.com/cyMccSb.jpg

Any help would be appreciated.

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Indexing tiles for pathfinding 1 Answer

Vector3.MoveTowards gives inconsistent speed when pathfinding between waypoints. 2 Answers

dijkstra's algorithm optimization 1 Answer

Specific 2D movement using Dijkstras Algorithm... 1 Answer

Implementation of Navmesh terrain dijkstra algorithm 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