To start off I followed this tutorial: Hex Map 1
Now I have been meddling with the code and I am trying to instantiate one building to the center of the a hexagon. The problem is that I am unsure how to send the hex position to my building creation script, or call the building creation script in the script where the grid is maintained.
The code (HexMapEditor.cs):
public List<GameObject> BuildingList = new List<GameObject>();
public HexGrid hexGrid;
private Color activeColor;
private int activeBuilding;
void Awake () {
SelectBuilding(0);
}
void Update () {
if (
Input.GetMouseButtonUp(0) &&
!EventSystem.current.IsPointerOverGameObject()
) {
HandleInput();
}
}
void HandleInput () {
Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(inputRay, out hit)) {
SelectColor(0);
hexGrid.CreateBuilding(hit.point, activeColor);
}
}
public void SelectBuilding(int index)
{
activeBuilding = index;
}
}
public void BuildingCreation(Vector3 Bpos)
{
Instantiate(BuildingList[activeBuilding], Bpos, Quaternion.identity);
}
The code (HexGrid.cs):
public Color defaultColor = Color.white;
public HexCell cellPrefab;
public Text cellLabelPrefab;
HexCell[] cells;
Canvas gridCanvas;
HexMesh hexMesh;
HexMapEditor Building;
void Awake () {
gridCanvas = GetComponentInChildren<Canvas>();
hexMesh = GetComponentInChildren<HexMesh>();
cells = new HexCell[height * width];
for (int z = 0, i = 0; z < height; z++) {
for (int x = 0; x < width; x++) {
CreateCell(x, z, i++);
}
}
}
void Start () {
hexMesh.Triangulate(cells);
}
private void OnTriggerEnter(Collider other)
{
Destroy(other.gameObject);
}
public void CreateBuilding(Vector3 position, Color color)
{
position = transform.InverseTransformPoint(position);
HexCoordinates coordinates = HexCoordinates.FromPosition(position);
int index = coordinates.X + coordinates.Z * width + coordinates.Z / 2;
HexCell cell = cells[index];
cell.color = color;
hexMesh.Triangulate(cells);
Building.BuildingCreation(position);
for (int i = 0; i < cells.Length; i++)
{
}
}
void CreateCell (int x, int z, int i) {
Vector3 position;
position.x = (x + z * 0.5f - z / 2) * (HexMetrics.innerRadius * 2f);
position.y = 0f;
position.z = z * (HexMetrics.outerRadius * 1.5f);
HexCell cell = cells *= Instantiate<HexCell>(cellPrefab);*
-
cell.transform.SetParent(transform, false);*
-
cell.transform.localPosition = position;*
-
cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z);*
-
cell.color = defaultColor;*
if (x > 0) {
-
cell.SetNeighbor(HexDirection.W, cells[i - 1]);*
-
}*
-
if (z > 0) {*
-
if ((z & 1) == 0) {*
-
cell.SetNeighbor(HexDirection.SE, cells[i - width]);*
-
if (x > 0) {*
-
cell.SetNeighbor(HexDirection.SW, cells[i - width - 1]);*
-
}*
-
}*
-
else {*
-
cell.SetNeighbor(HexDirection.SW, cells[i - width]);*
-
if (x < width - 1) {*
-
cell.SetNeighbor(HexDirection.SE, cells[i - width + 1]);*
-
}*
-
}*
-
}*
-
Text label = Instantiate<Text>(cellLabelPrefab);*
-
label.rectTransform.SetParent(gridCanvas.transform, false);*
-
label.rectTransform.anchoredPosition =*
-
new Vector2(position.x, position.z);*
-
label.text = cell.coordinates.ToStringOnSeparateLines();*
- }*
Please let met know if more code or information in required.