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 Mevee · Jan 14, 2015 at 01:01 PM · c#renderercolour

Turn-based strategy possible moves issue

I'm making a turn-based strategy game similar to advance wars. I've implemented a BFS algorithm that whenever a unit is selected, all the possible tiles it can go too will be highlighted. The tiles are prefabs that are dragged onto the scene and I have a tile manager script that locks them into coordinates (0,0), (1,0), (2,0) etc.

The issue I'm having is that some tiles are not highlighting when they should be. At first I thought this was an issue with my BFS algorithm (it still could be) but what I noticed that for the tiles that didn't highlight correctly if I remove them from the scene and then re-add them it would work until I restarted Unity.

BFS algorithm

 public override List<GameObject> getReachableTiles(GameObject currentTile, int moves) {
         currentTile = tm.GetTileAt ((int)currentTile.transform.position.x, (int)currentTile.transform.position.y).gameObject;
         List<GameObject> tempTileList = new List<GameObject>();
         if (!adjacentTiles.Contains(currentTile)) {
             adjacentTiles.Add (currentTile);
         }
         if (tm.GetTileAt (((int)currentTile.transform.position.x -1), (int)currentTile.transform.position.y) != null
             && (!adjacentTiles.Contains(tm.GetTileAt((int)currentTile.transform.position.x -1, (int)currentTile.transform.position.y).gameObject))){ //Check for tile left of current
             tempTileList.Add(tm.GetTileAt (((int)currentTile.transform.position.x -1), (int)currentTile.transform.position.y).gameObject);
             Debug.Log("Check Left");
         }
         if (tm.GetTileAt (((int)currentTile.transform.position.x +1), (int)currentTile.transform.position.y) != null
             && (!adjacentTiles.Contains(tm.GetTileAt((int)currentTile.transform.position.x +1, (int)currentTile.transform.position.y).gameObject))){ //Check for tile right of current
             tempTileList.Add(tm.GetTileAt (((int)currentTile.transform.position.x +1), (int)currentTile.transform.position.y).gameObject);
             Debug.Log("Check Right");
         }
         if (tm.GetTileAt (((int)currentTile.transform.position.x), ((int)currentTile.transform.position.y -1)) != null
             && (!adjacentTiles.Contains (tm.GetTileAt((int)currentTile.transform.position.x, (int)currentTile.transform.position.y-1).gameObject))){ //Check for tile below of current
             tempTileList.Add(tm.GetTileAt ((int)currentTile.transform.position.x, ((int)currentTile.transform.position.y -1)).gameObject);
             Debug.Log("Check Down");
         }
         if (tm.GetTileAt (((int)currentTile.transform.position.x), ((int)currentTile.transform.position.y +1)) != null
             && (!adjacentTiles.Contains (tm.GetTileAt((int)currentTile.transform.position.x, (int)currentTile.transform.position.y+1).gameObject))){ //Check for tile Above of current
             tempTileList.Add(tm.GetTileAt ((int)currentTile.transform.position.x, ((int)currentTile.transform.position.y +1)).gameObject);
             Debug.Log("Check Up");
         }
         if (moves > 0) {
             foreach(GameObject tiles in tempTileList) {
                 getReachableTiles (tiles, moves-1);
             }
             return adjacentTiles;
         }
         else {
             return adjacentTiles;
             }


Update function that changes the colour of the tiles for the possible moves of selected unit

 protected virtual void Update () {
         if (selected) {
             renderer.material.color = isSelected;
             possibleMoves = this.getReachableTiles (this.gameObject, this.getMove());
             foreach (GameObject possmove in possibleMoves)
             {
                 possmove.renderer.material.color = Color.gray;
 
             }
 
         }
         else {
             renderer.material.color = notSelected;
         }
     }



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 Mevee · Jan 14, 2015 at 11:40 PM 0
Share

thank you for your reply. I have it both in the editor and print statements that output the tile at the correct location during run time, but I will investigate further.

And thanks for the tip in the BFS algorithm!

avatar image DanSuperGP · Jan 15, 2015 at 12:08 AM 0
Share

If you're building a tile based game, I HIGHLY recommend the Grids by Gamelogic.

http://gamelogic.co.za/

It's brilliant, probably the single best add on for Unity I've ever used.

avatar image Mevee · Jan 15, 2015 at 12:15 AM 0
Share

Thank you for the recommendation, it looks really useful.

However I'm not sure how far I would take this project as I'm just trying to build program$$anonymous$$g expertise.

avatar image DanSuperGP · Jan 15, 2015 at 12:27 AM 0
Share

Understandable. I've used it on three little projects already, it's a nice thing to have in your stable of tricks.

Having a good grid implementation is one of the glaring problems Unity has in my estimation.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Baste · Jan 14, 2015 at 04:56 PM

Check if the tiles have been moved slightly. If you have a tile that's placed at x = 1.99999, casting it's x-coordinate to an int will yield 1, not 2. That will make things easier.

And also, you should really clean up your BFS. If you define the ints currentX and currentY in the top of getReachableTiles, and use them instead of "(int)currentTile.transform.position.x", it will be a lot easier to read.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

Why isnt renderer.isVisible not working? 1 Answer

Hiding/Revealing UI elements in Children 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