Hi,
I’ve been trying to make notes as I go on this course in case I forget what certain bits of code does after not touching it for a while and I’ve been having trouble keeping up with the last few grid lectures.
I’m hoping someone can break down the last few bits of the GridSystem script as that’s where I find it really hard to follow and summarise as we have so many words and variables with very similar names.
public Vector3 GetWorldPosition(GridPosition gridPosition) //Searches for and returns the X,Y,Z coordinates of the centre of the cell, based on cell size
{
return new Vector3(gridPosition.x, 0, gridPosition.z) * cellSize;
}
public GridPosition GetGridPosition(Vector3 worldPosition) //Translates the world position to grid coordinates
{
return new GridPosition(
Mathf.RoundToInt(worldPosition.x / cellSize), // Vector3 defaults to floats, whereas we need ints for our grid.
Mathf.RoundToInt(worldPosition.z / cellSize)
);
}
public void CreateDebugObjects(Transform debugPrefab) // Creates the GridDebugObject prefab across each calculated grid cell
{
for (int x = 0; x < width; x++)
{
for (int z = 0; z < height; z++)
{
GridPosition gridPosition = new GridPosition(x, z); // Uses the looped values to create cell 'gridPosition's
Transform debugTransform = GameObject.Instantiate(debugPrefab, GetWorldPosition(gridPosition), Quaternion.identity); // Spawns the GridDebugObject prefabs in the looped value coordinates
GridDebugObject gridDebugObject = debugTransform.GetComponent<GridDebugObject>(); // Accesses the GridDebugObject script and stores it in 'gridDebugObject'
gridDebugObject.SetGridObject(GetGridObject(gridPosition)); // ???
}
}
}
public GridObject GetGridObject(GridPosition gridPosition) // ???
{
return gridObjectArray[gridPosition.x, gridPosition.z];
}
So in essence, I need a bit more clarification on the ‘???’ bits. I’m also hoping my existing notes are correct…