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 JayFitz91 · Jan 04, 2016 at 09:49 PM · directionadjacent

Only choosing the surrounding tiles on a checkerboard

I just asked a question regarding this issue but unfortunately I'm still having issues. I want to choose a tile and then choose another tile but only if it is next to the first tile chosen. I am getting the following output:

alt text

Green tiles represent which tiles I am allowed to choose, red tiles indicate which ones i should not be allowed choose.

The tile in which I have highlighted is the first tile chosen. I want only to be able to chose the other 8 tiles that surround it, but as you can see, I am able to choose tiles further away from the starting one as shown by the green H on the board.

If anyone can help me out here, I'd appreciate it. My tiles are 3 spaces apart each and are 0.3 in scale along the x and z.

 //Checks if the ray has hit anything and if the user has clicked
         if (Physics.Raycast(ray, out hit) && Input.GetButtonDown("Fire1"))
         {
             if (hit.collider.tag == "Tiles")
             {
                 //nothing is selected
                 if (!isSelected)
                 {
                     firstTile = hit.transform.position;
                     hit.transform.GetComponent<Renderer>().material.color = Color.green;
                     isSelected = true;
                 }
                 else
                 {
                     Debug.Log(hit.transform.position);
                     Debug.Log(Mathf.Abs(firstTile.x - hit.transform.position.x));
 
                     if (Mathf.Abs(firstTile.x - hit.transform.position.x) == 3 || Mathf.Abs(firstTile.z - hit.transform.position.z) == 3)
                     {                      
                         hit.transform.GetComponent<Renderer>().material.color = Color.green;
                     }
                     else
                     {
                         hit.transform.GetComponent<Renderer>().material.color = Color.red;
                     }
                 }
             }
         } 

capture.png (44.4 kB)
Comment
Add comment · Show 3
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 ShadyProductions · Jan 04, 2016 at 09:51 PM 0
Share

wasn't my code working? also why are you checking the Z axis? is this not a 2D checkerboard? anyway try:

                      if ($$anonymous$$athf.Abs(firstTile.x - hit.transform.position.x) < 2 || $$anonymous$$athf.Abs(firstTile.z - hit.transform.position.z) < 2)
avatar image JayFitz91 ShadyProductions · Jan 04, 2016 at 10:01 PM 0
Share

Hey, I thought it was, I only checked it along the x-axis though, which worked fine, I then just tried to clean it up a bit with my current code above, but both yours and $$anonymous$$e are returning the same results when trying along the other axis.

Its actually going to be within a 3d world, so the z axis is the one I need to modify.

The code you posted returns the following image:

alt text

capture.png (43.7 kB)
avatar image ShadyProductions JayFitz91 · Jan 04, 2016 at 10:13 PM 0
Share

This might actually help. Try figure the logic out. :P alt text

2 Replies

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

Answer by ShadyProductions · Jan 04, 2016 at 10:24 PM

Back to my original code, but modified a bit by the picture.

     //bools outside of method
         Vector3 currentSelected;
         bool isSelected;
     
     //the rest inside of method
     if (Physics.Raycast(ray, out hit) && Input.GetMouseButtonDown(0))
     {
         if (hit.collider.tag == "Tiles" && !isSelected) {
             Debug.Log("test");
             isSelected = true;
             currentSelected = hit.transform.position;//grab its current pos
             hit.transform.GetComponent<Renderer>().material.color = Color.yellow;
         } else if (hit.collider.tag == "Tiles" && isSelected) {
             if ((currentSelected.x == hit.transform.position.x +1 && currentSelected.y == hit.transform.position.y +1) ||
                 (currentSelected.x == hit.transform.position.x +1 && currentSelected.y == hit.transform.position.y -1) ||
                 (currentSelected.x == hit.transform.position.x -1 && currentSelected.y == hit.transform.position.y -1) ||
                 (currentSelected.x == hit.transform.position.x -1 && currentSelected.y == hit.transform.position.y +1)) {
                 //4 corner adjacent tiles
                 Debug.Log("corner adjacent x:" + currentSelected.x + " y:" + currentSelected.y);
                 hit.transform.GetComponent<Renderer>().material.color = Color.green;
                 currentSelected = Vector3.zero;
             } else if ((currentSelected.x == hit.transform.position.x +1 && currentSelected.y == hit.transform.position.y) 
                        || (currentSelected.x == hit.transform.position.x -1 && currentSelected.y == hit.transform.position.y) 
                        || (currentSelected.y == hit.transform.position.y +1 && currentSelected.x == hit.transform.position.x) 
                        || (currentSelected.y == hit.transform.position.y -1 && currentSelected.x == hit.transform.position.x)) {
                 //4 directly adjacent tiles
                 Debug.Log("direct adjacent x:" + currentSelected.x + " y:" + currentSelected.y);
                 hit.transform.GetComponent<Renderer>().material.color = Color.green;
                 currentSelected = Vector3.zero; //delete this if you don't set isSelected to false
             } else {
                 Debug.Log("false x:" + currentSelected.x + " y:" + currentSelected.y);
                 hit.transform.GetComponent<Renderer>().material.color = Color.red;
             }
             //setting this to false again will make sure u have to click
             //another 2 tiles to check if you can keep checking the next tiles
             isSelected = false;  //< this ^
         }
     }

Works, I've tested it. =) alt text alt text


screenshot-1.png (7.5 kB)
screenshot-2.png (8.0 kB)
Comment
Add comment · Show 2 · 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 JayFitz91 · Jan 04, 2016 at 11:41 PM 0
Share

Hey man, thanks for this, appreciate the time you took for me, i cant try this right now but you seem to have tested and got it working :P Thanks again

avatar image ShadyProductions JayFitz91 · Jan 04, 2016 at 11:52 PM 0
Share

Yeah no problem. =)

avatar image
0

Answer by bubzy · Jan 04, 2016 at 11:04 PM

or you could use a 2d array

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

34 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

Related Questions

Getting 3rd person to look in direction of camera when right mouse clicked 1 Answer

Relative Movement Problem 1 Answer

Instantiating a projectile pointing at target. 1 Answer

EARTH CALLING ANSWER!!!! 1 Answer

Making My Raycast Face The Right Direction 3 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