Hello, Noob here. I have a 2d project where I want to create a hex grid for unit placement. Currently I have a script that creates a hex mesh and then uses that mesh to create a hex grid.
I also implemented a mouse over color change. Which worked great. Then I switched it from reacting to the mouse pointer to reacting to a single hex I moved with the mouse. Again, worked great.
But what I really want is for it to react to multiple hexes, as the units will have varying sizes. Smallest unit may be 1 hex but a larger unit might take 3 or 7 hexes of space. At my current level of Unity noobness I can’t figure out how to do this, nor the best way to go about it.
Currently I am applying a script to each individual grid hex that checks for a raycast and changes the color. This was based on an example I found to do the mouse over.
My idea was to create a group of ‘selection hexes’ that matched the size of the unit and as I move it around it would highlight a legitimate location to place it (i.e. unit size 7 finds 7 empty hexes). No idea if this is the way to go about it and currently what I have doesn’t work anyway. I just took my working single example and put it in a loop. But, again, it doesn’t work, it just highlights 1 hex at a time.
Any advice on how to best implement this? Best practices?
Code attached to hexes in the grid:
using UnityEngine;
[RequireComponent(typeof(Collider))]
public class GridHex : MonoBehaviour {
Color normalColor;
GameObject[] selectors;
//public Transform selectionObject;
void Awake() {
selectors = GameObject.FindGameObjectsWithTag("selectortile");
//selectionObject = GameObject.Find("HexagonSelectorTile").transform;
}
void Start() {
normalColor = GetComponent<Renderer>().material.color;
}
void Update () {
foreach (GameObject selector in selectors) {
//Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//Ray ray = new Ray(selectionObject.position, Vector3.forward);
Ray ray = new Ray(selector.transform.position, Vector3.forward);
RaycastHit hitInfo;
Debug.DrawRay(ray.origin, ray.direction, Color.green);
if (GetComponent<Collider>().Raycast(ray, out hitInfo, Mathf.Infinity)) {
GetComponent<Renderer>().material.color = UnityEngine.Color.red;
} else {
GetComponent<Renderer>().material.color = normalColor;
}
}
}
}
What it looks like:

