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
1
Question by Macknooze · Jul 04, 2015 at 05:05 AM · javascriptarraypathfindingtile

Pathfinding. Does not "walk" on every tile.

Hello,

I'm working on a pathfinder of type A*, but I'm having some trouble trying to iterate each move.

Here's the code I've got so far:

 while (tilesForSearching.length > 0) //While there is tiles to be searched...
     {
         for(var tile : GameObject in tilesForSearching) //...do this to each one...
         {
             var refTile = tile.GetComponent.<IndividualTileScript>(); //Reference for convenience.
             
             refTile.state = 2; //...state of the tile is set to 2 (searching)...
             tile.GetComponent.<SpriteRenderer>().color = Color.yellow; //Turns it yellow when it has been walked over.
         
         
         
         
         
             //...all adjacent tiles is added to tilesForSearching...
             if(refTile.adjacentTile_upper && refTile.adjacentTile_upper.GetComponent.<IndividualTileScript>().state == 0)
             {
                 tilesForSearching.Add(refTile.adjacentTile_upper);
             }
             
             if(refTile.adjacentTile_lower && refTile.adjacentTile_lower.GetComponent.<IndividualTileScript>().state == 0)
             {
                 tilesForSearching.Add(refTile.adjacentTile_lower);
             }
             
             if(refTile.adjacentTile_right && refTile.adjacentTile_right.GetComponent.<IndividualTileScript>().state == 0)
             {
                 tilesForSearching.Add(refTile.adjacentTile_right);
             }
             
             if(refTile.adjacentTile_left && refTile.adjacentTile_left.GetComponent.<IndividualTileScript>().state == 0)
             {
                 tilesForSearching.Add(refTile.adjacentTile_left);
             }
             
     
             
             //...removes itself from tilesForSearching...
             tilesForSearching.Shift();
         }
     }

It checks if the variable state is equal to 0, which means that it is open and walkable.
At the moment, every tile is set to 0/open.
So in theory every tile should become yellow, this is not the case.

alt text

For some reason the upper right tiles are not yellow.

I think the fault may be when it's trying to remove the tile from the array, but in all honesty - I have no idea why it's not working.

skjermbilde-2015-07-04-kl-002818.png (12.6 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
0

Answer by Macknooze · Jul 04, 2015 at 06:39 PM

I figured it out!

As I suspected it was because the tiles wasn't properly removed from the array. This is the new code:

 while (tilesForSearching.length > 0) //While there is tiles to be searched...
     {
         for(var tile : GameObject in tilesForSearching) //...do this to each one...
         {
             var refTile = tile.GetComponent.<IndividualTileScript>(); //Reference for convenience.
             
             refTile.state = 2; //...state of the tile is set to 2 (searching)...
             tile.GetComponent.<SpriteRenderer>().color = Color.yellow;
         
         
         
         
         
             //...all adjacent tiles' states becomes 2 = searching and add them to tilesForSearching...
             if(refTile.adjacentTile_upper && refTile.adjacentTile_upper.GetComponent.<IndividualTileScript>().state == 0)
             {
                 tilesForSearching_temporary.Add(refTile.adjacentTile_upper);
             }
             
             if(refTile.adjacentTile_lower && refTile.adjacentTile_lower.GetComponent.<IndividualTileScript>().state == 0)
             {
                 tilesForSearching_temporary.Add(refTile.adjacentTile_lower);
             }
             
             if(refTile.adjacentTile_right && refTile.adjacentTile_right.GetComponent.<IndividualTileScript>().state == 0)
             {
                 tilesForSearching_temporary.Add(refTile.adjacentTile_right);
             }
             
             if(refTile.adjacentTile_left && refTile.adjacentTile_left.GetComponent.<IndividualTileScript>().state == 0)
             {
                 tilesForSearching_temporary.Add(refTile.adjacentTile_left);
             }
             
             
             
             //...adjacent tiles gets a copy of this tile's tilePath, and adds themselves...
             
             
             
             
             //...removes itself from tilesForSearching...
             //tilesForSearching.Shift();
             
             
         }
         
         tilesForSearching.Clear();
         tilesForSearching = tilesForSearching.Concat(tilesForSearching_temporary);
         tilesForSearching_temporary.Clear();
         
         testing++;
     }

I simply put them in a temporary array and made sure the main one was empty before adding the new ones. it works perfectly now!

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

2 People are following this question.

avatar image avatar image

Related Questions

array of boxes 2 Answers

array out of range in simple pathinder javascript 1 Answer

How can i show the texture in the array? 2 Answers

Push once then keep updating. 1 Answer

boolean issue 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