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 yigitkahramn · Oct 26, 2018 at 01:02 PM · movementgrid based game

Getting Valid Moves On A Grid

Hey! I have a problem that I'm struggling with for days. I'm currently working on a Turn Based Tactics project, I want to highlight valid moves for a character according to character's movement points (like every other TBS do). I have some of Breadth First Search working but it doesn't return me what I want. I'll provide what my node variables are here. Nodes(which are squares) have all their 8 neighbours listed with them. via this

 public List<Node> GetNeighbours(Node node, GridMaster gridMaster) {
         neighbours = new List<Node>();
         for(int x = -1; x <= 1; x++) {
             for(int z = -1; z <= 1; z++) {
                 if(x == 0 && z == 0) {
 
                 } else {
                     int cx = node.x + x;
                     int cy = 0;
                     int cz = node.z + z;
                     if(cx >= 0 && cx < gridMaster.width && cz >= 0 && cz < gridMaster.depth) {
                         neighbours.Add(gridMaster.GetNode(cx, cy, cz));
                     }
                 }
             }
         }
         return neighbours;
     }

in a different class I have a methods like this:

 //It's for calculating Vector3 Distance Not for A* Pathfinder.
     public int GetDistance(Node nodeA, Node nodeB) {
         int retVal = Mathf.Abs(nodeA.x - nodeB.x) + Mathf.Abs(nodeA.z - nodeB.z);
         return retVal;
     }
     //Returns 1 if nodes are neighbours, otherwise returns cost according to distance;
     public int GetMovementCost(Node start, Node end) {
         if (GetDistance(start, end) == 1) {
             return 1;
         } else {
             int distance = GetDistance(start, end);
             int cost = 1;
             for (int i = 0; i <= distance; i++) {
                 cost++;
             }
             return cost;
         }
     }

and finally my Breadth First Search implementation is this:

 public List<Node> GetPossibleDestinations(Node startNode, int movementPoints) {
         Queue<Node> frontier = new Queue<Node>();
         List<Node> possibles = new List<Node>();
         frontier.Enqueue(startNode);
         possibles.Add(startNode);
         int cost = 1;
         while (frontier.Count > 0) {
             Node current = frontier.Dequeue();
             for (int i = 0; i <= movementPoints; i++) {
                 foreach (Node neighbour in current.neighbours) {
                     cost += GetMovementCost(neighbour, current);
                     if (cost <= movementPoints) {
                         if (!possibles.Contains(neighbour) && neighbour != current) {
                             frontier.Enqueue(neighbour);
                             possibles.Remove(startNode);
                             possibles.Add(neighbour);
                         }
                     }
                 }
             }
         }
         return possibles;
     }

But in the end all I have is startNodes neighbours, even I raise movement points to 1000. May anyone can help me with that?

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 yigitkahramn · Oct 26, 2018 at 04:43 PM

 public List<Node> GetPossibleDestinations(Node startNode, int movementPoints, GridMaster gridMaster) {
         Queue<Node> frontier = new Queue<Node>();
         List<Node> possibles = new List<Node>();
         frontier.Enqueue(startNode);
         possibles.Add(startNode);
         int[, , ] cost = new int[gridMaster.width, gridMaster.height, gridMaster.depth];
         for (int x = 0; x < gridMaster.width; x++) {
             for (int y = 0; y < gridMaster.height; y++) {
                 for (int z = 0; z < gridMaster.depth; z++) {
                     cost[x, y, z] = int.MaxValue;
                 }
             }
         }
         cost[startNode.x, startNode.y, startNode.y] = 0;
         while (frontier.Count > 0) {
             Node current = frontier.Dequeue();
             int c = cost[current.x, current.y, current.z];
             foreach (Node neighbour in current.neighbours) {
                 c += neighbour.MovementCost;
                 if (c <= movementPoints) {
                     if (!possibles.Contains(neighbour) && neighbour != current) {
                         frontier.Enqueue(neighbour);
                         cost[neighbour.x, neighbour.y, neighbour.z] = c;
                         possibles.Remove(startNode);
                         possibles.Add(neighbour);
                     }
                 }
             }
         }
         return possibles;
     }

That is so far I've achieved. Although its base is working, search is not uniform and tend to go left side. like in these images.

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

147 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

Related Questions

Grid-based movement with auto-move 1 Answer

Click to move in a 3d-blockbased single mesh 1 Answer

Grid Based Movement System AI freezing? 1 Answer

Problem with raycast detection? 1 Answer

Grid-based movement *without* smooth movement between tiles? 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