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
0
Question by Jarsh13 · Apr 13, 2013 at 06:01 AM · arrayspathfindinggrid

A* Pathfinding Grid

I want to create a path finding system for my tower defense game. I already tried to create it but it didnt turn out how I wanted it. I understand how A* works I simply want to find a better way to implement it the second time around. The big problems with my last attempt is that I had trouble accessing my grid. What is the best way to store a grid and access it? Second biggest problem was finding a way to search through my grid to find neiboring squares from a given point. What I came up with was really messy and inefficient. any tips you guys can give me would be great.

ps. I dont want to use any A* or path finding tools I want to do as much of this on my own as possible so I can learn more about unity and programming in general.

Comment
Add comment · Show 4
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 whydoidoit · Apr 13, 2013 at 06:01 AM 0
Share

What did you do to store the connections?

avatar image BertrandT · Apr 13, 2013 at 02:59 PM 0
Share

How did you do it the first time? you don't have to use an array. Another way would be using a Node class, that has a List of 'Node' containing the connecting nodes. Or create an Edge Class that links two Nodes together.

avatar image Jarsh13 · Apr 13, 2013 at 05:31 PM 0
Share

What your saying sounds similar to the fps tutorial path finding on unity is that correct?

avatar image whydoidoit · Apr 13, 2013 at 07:30 PM 0
Share

So if it's really a grid then a grid of classes representing each cell with data about the height of the land etc is the best way to go. If it's a list of way points then storing the connected nodes is useful in a list. I describe my grid technique in this article: http://unitygems.com/astar-1-journeys-start-single-step/

1 Reply

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

Answer by Graham-Dunnett · Apr 13, 2013 at 05:03 PM

How about something like this:

 // c# example
 using UnityEngine;
 using System.Collections;
 
 public class grid : MonoBehaviour {
 
     static int _width = 10;
     static int _height = 10;
     
     static int[,] _grid;
     
     bool CanMoveNorth(int x, int y) {
         if (y == 0) return false; // already on top edge
         if (_grid[x,y-1] == 0) return true;
         return false;
     }
     bool CanMoveSouth(int x, int y) {
         if (y == _height-1) return false; // already on bottom edge
         if (_grid[x,y+1] == 0) return true;        
         return false;
     }
     bool CanMoveEast(int x, int y) {
         if (x == _width-1) return false; // on right edge
         if (_grid[x+1,y] == 0) return true;        
         return false;
     }
     bool CanMoveWest(int x, int y) {
         if (x == 0) return false; // on left edge
         if (_grid[x-1,y] == 0) return true;
         
         return false;
     }
     
     // Use this for initialization
     void Start () {
         _grid = new int[_width, _height];
         
         for (var i = 0; i < _height; i++) {
             for (var j = 0; j < _width; j++) {
                 _grid[j,i] = 0;
             }
         }
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

I'm using a 2-dimensional array in c#. Javascript does not have an easy 2d array. The 2d array stores an int at each location. If the grid has a zero, then the cell can be used as a path. I assume in your game you have blockers at grid locations, so write a non-zero value into the cell. The four functions I have written look to see if you can move north, east, south or west from a given location. There are two tests to make, first, if the cell is already on the edge in which case you cannot move off the grid. Otherwise, look to see if the adjacent cell has a zero or not.

Comment
Add comment · Show 1 · 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 Jarsh13 · Apr 13, 2013 at 05:24 PM 0
Share

Thats kinda of like what I did last time except I stored my grid in a 2d array that stored a GameObject ins$$anonymous$$d of an int. If I had a character that wanted to find a location though what would be the best way to find were he is on the grid using your method?

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

13 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

Related Questions

Stuck with A* Pathfinding Script 3 Answers

Making an Array of Tiles from Unity's Grid TileMap feature? 0 Answers

Grid Generation 2 Answers

2D Grid Movement (turn based) 2 Answers

Grid obstacle detection 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