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:
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;
}
}
}
}