Is there a way to determine in advance from which side the tile from the player? enter image description here

Prefabs structure:

one:

LeftTile Tile LeftAttachPoint TopAttachPoint 

2:

  TopTile Tile LeftAttachPoint TopAttachPoint 

Script:

 public class TileManager : MonoBehaviour { public GameObject [] prefabs; public GameObject currentTile; // Use this for initialization void Start () { for (int i = 0; i < 100; i++) { SpawnTile(); } } void SpawnTile() { int RandomIndex = Random.Range(0,2); currentTile = (GameObject) Instantiate(prefabs[RandomIndex],currentTile.transform.GetChild(0).transform.GetChild(RandomIndex).position, Quaternion.identity); } } 

    2 answers 2

    If the Y coordinates of the center of the tile are greater than the coordinates of the player, then the tile is on top, if less, then the bottom. If the coordinates of the tile in X are greater than the coordinates of the X player, then the tile on the right, if less, then on the left.

    • moving along the coordinates. Π’Π»Π΅Π²ΠΎ (-X) , прямо (Z) That is, if on the left side of the Tile Center (-X) < Player Center (X) and right, Tile Center (-Z) < Player Center (-Z) right? - Kill Noise
    • In theory, yes, but experiment and check if this is what you need or not - BogdanBida

    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; } } 
    • can show in the form of a code? - Kill Noise
    • one
      Ok, now I 'll edit the answer - Fan4i
    • Why is the Point highlighted in red? - Kill Noise
    • error CS0246: The type or namespace name 'Point' could not be found. Are you missing an assembly reference? ` - Kill Noise
    • You do not have a Point Script, of course, I just thought it was already provided for you - Fan4i