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
1
Question by JayFitz91 · Jan 04, 2016 at 06:08 PM · coloradjacent

Check if two objects are next to each other?

I have a checkerboard style prefab in my scene [see below] I would like to select a tile (which turns it red) then select another tile and only turn it red if it is next to the currently selected tile.

Is there a way I can do this?

I have the following code to turn the tiles red:

 //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")
             {
                 //Change the tile color to red
                 hit.transform.GetComponent<Renderer>().material.color = Color.green;
             }
         }

It should be noted that each tile is a separate object

alt text

capture.png (13.9 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

2 Replies

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

Answer by ShadyProductions · Jan 04, 2016 at 06:15 PM

Is the checkboard instantiated at runtime? If so you can add all the objects to a list and loop through it and check each tile's position in 4 directions, north, east, south, west to see if they are +1 or -1 x or y to the corresponding tile.

If they aren't instantiated at runtime you'll have to loop through the checkerboard and add them to a list first.

Thats how I would do this.

Comment
Add comment · Show 4 · 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 06:27 PM 0
Share

It is not instantiated at runtime unfortunately, but ill add it to a list and give what you said a do, cheers

avatar image ShadyProductions JayFitz91 · Jan 04, 2016 at 06:43 PM 1
Share

Actually I reread your question, and it's actually simpeler to do.. Put currentSelected and isSelected in your class not in the method, and set isSelected to false in the start method. :)

         Vector3 currentSelected;
         bool isSelected;
                  if (Physics.Raycast(ray, out hit) && Input.GetButtonDown("Fire1"))
                  {
                      if (hit.collider.tag == "Tiles" && !isSelected) //if nothing is selected
                      {
                          isSelected = true;
                          currentSelected = hit.transform.position //grab its current pos
                          hit.transform.GetComponent<Renderer>().material.color = Color.green;
                      }
         
                      if (hit.collider.tag == "Tiles" && isSelected) //if one is already selected
                      {
 //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 ^
                          if ((currentSelected.x == hit.transform.position.x +1) || (currentSelected.x == hit.transform.position.x -1) || (currentSelected.y == hit.transform.position.y +1) ||currentSelected.y == hit.transform.position.y -1)) {
                          currentSelected = null; //delete this if you don't set isSelected to false
                          //do whatever with the tile that is next to it.
                           }
                      }
                  }

I believe this should work, however it is untested. Sorry if it is a bit sloppy.

avatar image JayFitz91 ShadyProductions · Jan 04, 2016 at 07:15 PM 0
Share

Ah yes, I see what you're doing here, I wasn't aware of what exactly what was going on at first, but I understand now, thanks a million for your time, this is just what I need :)

Show more comments
avatar image
0

Answer by toddisarockstar · Jan 04, 2016 at 07:00 PM

          var i:int;
          var redsquares=new Array();
          function Update () {
           //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")
                       {
 //loop through the array of redsquare positions and check if one is close.
 //if so, add the new position to the array!
 
                           i=redsquares.length;
                        while(i>0){i--;
                          if(Mathf.Abs(hit.transform.position.x-redsquares[i].x)<2){
                          if(Mathf.Abs(hit.transform.position.y-redsquares[i].y)<2){
                          if(hit.transform.position!=redsquares[i]){
                           redsquares.Push(hit.transform.position);}
                           i=0;
                           //Change the tile color to red
                          hit.transform.renderer.material.color=Color.red;}}}
                       }
                   }
          }

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

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

35 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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

How to cycle through color spectrum with material? 1 Answer

new Color() not working properly? 1 Answer

Trail faded at start - All colors are full alpha 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