Suppose you have a script attached to a player who sets his grid position, here you have a similar situation click . Only here a check on the tiles diagonally, for your player, the tile with coordinates (2.1) will be Tile Forward, and Tile Left - (1.0), respectively. Here there is one snag, the tiles have their own positions in the global (imaginary) grid and a sheet has been created in which the values ββof the existing positions of the tiles are entered. Thus, the check includes only the condition on the content in the sheet of the point that you want to check for whether it is on the right or in front of the player, and assigning to a local array of two cells, the positions to be checked (for further work with them). Sample scripts:
PlayerScript:
public class PlayerScript: MonoBehaviour{ public Point GridPosition { get; set; } public void SetGridPosition(Point gridPos){ this.GridPosition = gridPos; //ΠΡΠΎΡ ΠΌΠ΅ΡΠΎΠ΄ Π½ΡΠΆΠ΅Π½ Π΄Π»Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΊΠΈ Π½ΠΎΠ²ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ ΠΈΠ³ΡΠΎΠΊΠ° ΠΏΡΠΈ ΠΏΠ΅ΡΠ΅ΠΌΠ΅ΡΠ΅Π½ΠΈΠΈ Π½Π° ΡΠ°ΠΉΠ» Ρ ΡΠ°ΠΊΠΎΠΉ-ΡΠΎ ΠΏΠΎΠ·ΠΈΡΠΈΠ΅ΠΉ. } }
TileScript
When creating clones of tiles from a prefab in the hierarchy, we can assign our positions to them relative to the location in the imaginary grid and make references to objects by adding a sheet of tiles:
public class TileScript: MonoBehaviour{ public Point GridPosition { get; set; } public void SetTileGridPosition (Point gridPos, GameObject Tile, Vector3 newTilePos){ this.GridPosition = gridPos; Tile.transform.position = newTilePos; GameManager.TilesArray.Add (Tile); GameManager.AllTilesPoints.Add (gridPos); } }
Gamemanager
Now, when creating tiles, we can access their script and put them in the right positions:
public static List<GameObject> TilesArray = new List<GameObject>(); public static List<Point> AllTilesPoints = new List<Point>(); [SerializeField] private GameObject TilePrefab; private float TileWidthOffset; //...ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½Π°Ρ Π΄Π»Ρ ΡΠ°ΡΡΡΡΡΠ° ΡΠΈΡΠΈΠ½Ρ ΡΠ°ΠΉΠ»Π° private float TileHeightOffset;//...ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½Π°Ρ Π΄Π»Ρ ΡΠ°ΡΡΡΡΡΠ° Π²ΡΡΠΎΡΡ ΡΠ°ΠΉΠ»Π° void Start(){ SetTileOffsets(); CreateTiles (TilePrefab); } private void CreateTiles(GameObject TilePrefab){ Vector3 TilesStart = new Vector3 (0f,0f,0f); //Π‘ ΠΊΠ°ΠΊΠΎΠΉ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ ΠΌΡ Π±ΡΠ΄Π΅ΠΌ Π½Π°ΡΠΈΠ½Π°ΡΡ ΡΠΏΠ°Π²Π½ΠΈΡΡ ΡΠ°ΠΉΠ»Ρ //ΠΠ΄Π΅ΡΡ Π²ΠΎΠ·ΠΌΠΎΠΆΠ΅Π½ Π»ΡΠ±ΠΎΠΉ Π΄ΡΡΠ³ΠΎΠΉ ΡΠΈΠΊΠ» for ΡΠΎΠ·Π΄Π°Π½ΠΈΡ ΡΠ°ΠΉΠ»ΠΎΠ², ΡΡΠΎ ΡΠΆΠ΅ Π½Π° ΡΡΠΌΠΎΡΡΠ΅Π½ΠΈΡ ΡΠ²ΠΎΠ΅ΠΉ Π·Π°Π΄ΡΠΌΠΊΠΈ, ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΈ ΠΊΠ°ΠΊ Π½ΡΠΆΠ½Ρ ΡΠ°ΡΠΏΠΎΠ»ΠΎΠΆΠΈΡΡ ΡΠ°ΠΉΠ»Ρ //ΠΡΠΈΠΌΠ΅Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ ΡΠ΅ΡΠΊΠΈ ΠΈΠ· ΠΏΡΡΠΈ ΡΠ°ΠΉΠ»ΠΎΠ² for (int y = 0; y < 5; y++) { for (int x = 0; x < 5; x++) { GameObject Tile = Instantiate (TilePrefab); Vector3 newTilePos = new Vector3(TilesStart.x + (TileWidthOffset * x), TilesStart.y+ (TileHeightOffset * y), 0f); Tile.GetComponent<TileScript> ().SetTileGridPosition (new Point (x, y), Tile, newTilePos ); } } } private void SetTileOffsets(){ //....ΠΠ΄Π΅ΡΡ ΡΠ²ΠΎΠΉ ΠΌΠ΅ΡΠΎΠ΄, ΡΠ°ΡΡΡΠΈΡΡΠ²Π°ΡΡΠΈΠΉ Π²ΡΡΠΎΡΡ / ΡΠΈΡΠΈΠ½Ρ ΡΠ²ΠΎΠ΅Π³ΠΎ ΡΠ°ΠΉΠ»Π° Π΄Π»Ρ ΡΠ°ΠΉΠ» ΠΎΡΡΡΠ΅ΡΠ°.... } private void GoOverTilesArray(Point TileCheckPoint){ for (int i = 0; i < TilesArray.Count; i++) { if (TileCheckPoint = TilesArray [i].GetComponent<TileScript> ().GridPosition) { Debug.Log ("ΠΡΠΎ ΡΠ°ΠΉΠ» Ρ ΠΏΠΎΠ·ΠΈΡΠΈΠ΅ΠΉ: " + TileCheckPoint.X + ", " + TileCheckPoint.Y); } } }
Something like this. In the future, we can create a verification method, for example, by adding an event to OnMouseDown () in the player's script:
private void OnMouseDown(){ CheckTilesDirections (); } private void CheckTilesDirections(){ //ΠΠ΅ΡΠΎΠ΄ ΠΏΡΠΎΠ²Π΅ΡΠΊΠΈ Π½Π°ΠΏΡΠ°Π²Π»Π΅Π½ΠΈΡ ΡΠ°ΠΉΠ»Π° Point PlayerPoint = Player.GetComponent<PlayerScript>().GridPosition; float x = PlayerPoint.X; float y = PlayerPoint.Y; Point TileCheckPoint = new Point (x, y); //ΠΡΠΎΠ²Π΅ΡΡΠ΅ΠΌ ΡΡΡΠ΅ΡΡΠ²ΠΎΠ²Π°Π½ΠΈΠ΅ ΠΏΠΎΠΉΠ½ΡΠ° Π² Π»ΠΈΡΡΠ΅ ΠΈ Π΅ΡΠ»ΠΈ ΠΎΠ½ ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ, ΡΠΎ ΠΊΠ°ΠΊΠΎΠΉ ΡΡΠΎ ΡΠ°ΠΉΠ» //ΠΠΎΠΏΡΡΡΠΈΠΌ ΠΌΡ ΠΏΡΠΎΠ²Π΅ΡΡΠ΅ΠΌ ΠΊΠ²Π°Π΄ΡΠ°Ρ ΠΏΠΎΠ·ΠΈΡΠΈΠΉ Π²ΠΎΠΊΡΡΠ³ ΠΈΠ³ΡΠΎΠΊΠ° for (y + 1; y >= (y - 1); y--) { for (x - 1; x <= (x + 1); x++) { TileCheckPoint = new Point (x, y); if (GameManager.AllTilesPoints.Contains (TileCheckPoint)) { GameManager.GoOverTilesArray (TileCheckPoint); //ΠΠ΄Π΅ΡΡ ΠΌΡ ΠΏΡΠΎΡΡΠΎ Π²ΡΠ²ΠΎΠ΄ΠΈΠΌ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ ΡΠ°ΠΉΠ»ΠΎΠ² Π² ΠΊΠ²Π°Π΄ΡΠ°ΡΠ΅ ΠΏΠΎΠ·ΠΈΡΠΈΠΉ, ΠΎΠΊΡΡΠΆΠ°ΡΡΠΈΡ
ΠΈΠ³ΡΠΎΠΊΠ°. //Π’Π°ΠΊΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ, ΠΏΡΠΎΠΉΠ΄ΡΡΡ ΡΠ΅ΡΠ΅Π· ΠΌΠ°ΡΡΠΈΠ² ΡΠ°ΠΉΠ»ΠΎΠ² ΠΈ ΡΠΎΠΏΠΎΡΡΠ°Π²ΠΈΠ² ΠΈΡ
ΠΏΠΎΠΉΠ½Ρ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ Π² ΡΠ΅ΡΠΊΠ΅ Ρ Π½ΡΠΆΠ½ΠΎΠΉ Π½Π°ΠΌ Π΄Π»Ρ ΠΏΡΠΎΠ²Π΅ΡΠΊΠΈ ΠΏΠΎΠ·ΠΈΡΠΈΠ΅ΠΉ, //ΠΌΡ Π²ΡΠ²ΠΎΠ΄ΠΈΠΌ Π² ΠΊΠΎΠ½ΡΠ»Ρ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ ΠΏΠΎΠ·ΠΈΡΠΈΠΈ ΡΡΠΎΠ³ΠΎ ΡΠ°ΠΉΠ»Π°. ΠΠ°Π»ΡΡΠ΅ ΡΠΆΠ΅ Π΄ΠΎΠ΄ΡΠΌΠ°ΡΡ ΠΊΠΎΠ΄ Π΄Π»Ρ ΠΏΡΠΎΠ²Π΅ΡΠΊΠΈ ΡΠ°ΠΉΠ»Π° ΡΠΏΡΠ°Π²Π° ΠΈ ΡΠΏΠ΅ΡΠ΅Π΄ΠΈ ΠΈΠ³ΡΠΎΠΊΠ°, //Ρ Π΄ΡΠΌΠ°Ρ Π½Π΅ ΡΠΎΡΡΠ°Π²ΠΈΡ ΡΡΡΠ΄Π°. ΠΠΎΠ΄ΠΊΠΈΠ½Ρ ΠΌΡΡΠ»Ρ: if ((TileCheckPoint.X= PlayerPoint.X) && (TileCheckPoint.Y=PlayerPoint.Y+1)) - ΡΠ°ΠΉΠ» ΡΠ»Π΅Π²Π° Π² ΡΠ²ΠΎΡΠΌ ΡΠ»ΡΡΠ°Π΅ //if ((TileCheckPoint.X = PlayerPoint.X+1) && (TileCheckPoint.Y = PlayerPoint.Y)) - ΡΠ°ΠΉΠ» Π²ΠΏΠ΅ΡΠ΅Π΄ΠΈ ΠΈΠ³ΡΠΎΠΊΠ° } } } }
Of course, everything is specific here, and you can choose an option to work with your idea by analogy. Some kind of too detailed description turned out, up to instantiation methods with specific positions, well, then, itβs up to you to decide how to apply this information. UPD: Point.cs:
using System.Collections; using System.Collections.Generic; using UnityEngine; public struct Point { public int X {get; set;} public int Y {get; set;} public Point(int x, int y){ this.X = x; this.Y = y; } }