Hi, I’m just working on an auto tiling system. I have most of the logic figured in my head in regards to calculating what sprite changes in to what. I just can’t figure out how I would go about getting a game object based on the position in relation to the object that this script is running on.
A picture might help me explain:
- All of the squares in the image are instances of terrain prefabs e.g. grass, sand, water. They all run the same script.
- The green square in the centre represents the game object that the script is running on. It is just a sprite with a script.
- The blue squares surrounding the green square represent game objects and the position of them in relation to the green square.
The code below is the loop which cycles through the array in which the game objects are stored. It then runs the autotile script attached to the game objects one by one.
The array size is also determined by the size of the map because that’s how many tiles there are.
terrainIndex = 0;
for (terrainIndex = 0; terrainIndex < mapWidth * mapHeight; terrainIndex++)
{
terrainObject[terrainIndex].GetComponent<AutoTile>().StartAutoTile ();
}
So now what I’m trying to figure out is how to get the green square to to check each position and compare the game object that was found with its self.
How does one find a game object base on its position?
I’ve read about using Physics.OverlapSphere but i would rather not have any collisions on the prefabs.
Does any one have any ideas?
Additional info
I have achieved this before using GameMaker but I feel C# and unity is the way forward with this project.
This is the code in GML I used to get the sprites surrounding the centre sprite.
iw = sprite_width;
w_left = place_meeting(x-iw,y,object_index);
w_right = place_meeting(x+iw,y,object_index);
w_up = place_meeting(x,y-iw,object_index);
w_down = place_meeting(x,y+iw,object_index);
w_upleft = place_meeting(x-iw,y-iw,object_index);
w_downleft = place_meeting(x-iw,y+iw,object_index);
w_upright = place_meeting(x+iw,y-iw,object_index);
w_downright = place_meeting(x+iw,y+iw,object_index);
- place_meeting basically checks the for an object at the location provided but I think it does use collision.
Thanks!



