- Home /
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

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.
It is not instantiated at runtime unfortunately, but ill add it to a list and give what you said a do, cheers
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.
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 :)
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;}}}
}
}
}
Your answer