У меня странная ошибка, я не понимаю, почему это происходит, может кто-то может помочь.
Я делаю змейку, а я - банкомат Grid. Я создаю экземпляр сетки в зависимости от размера игрового объекта, однако при создании экземпляра сетки есть смещение. это не то же самое, что размер игрового объекта, который я указал (черный ящик, как показано на изображении ниже).
Вот мой менеджер, где я мгновенно загружаю сетку
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridManager : MonoBehaviour {
[SerializeField] private GridTile gridTilePrefab;
[SerializeField] private Transform playArea;
[SerializeField] private int gridSize = 10;
public int GridSize {
get { return gridSize; }
}
private Vector3 startPoint;
public Vector3 StartPoint {
get { return startPoint; }
}
private int width;
private int height;
private Transform[,] grid;
public void InitializeGrid()
{
// Grid Size
width = Mathf.RoundToInt (playArea.localScale.x * gridSize);
height = Mathf.RoundToInt (playArea.transform.localScale.y * gridSize);
grid = new Transform[width,height];
// get the left bottom as start point
startPoint = playArea.GetComponent<Renderer>().bounds.min;
CreateGridTiles ();
}
private void CreateGridTiles()
{
if (gridTilePrefab == null)
return;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// Get world pos of grid location
Vector3 worldPos = GetWorldPos(x,y);
GridTile gridTile;
gridTile = Instantiate (gridTilePrefab, worldPos , Quaternion.identity);
gridTile.name = string.Format ("Tile({0},{1})" , x , y );
grid [x, y] = gridTile.transform;
}
}
}
private Vector3 GetWorldPos (int x, int y)
{
float xf = x;
float yf = y;
return new Vector3 (startPoint.x + (xf / gridSize) ,startPoint.y + (yf / gridSize) ,startPoint.z);
}
// Use this for initialization
void Start () {
InitializeGrid ();
}
}





Ошибка была связана не с кодом, а со спрайтом плитки. Я изменил его ось с центра на Внизу влево. Поскольку в моем коде это startPoint = playArea.GetComponent<Renderer>().bounds.min;
, он будет занимать нижний левый угол объекта.