- Home /
Unity 5 - Hex Tile change color on mouseover
I am trying to change the color of the tile I made on mouse over but cant seem to get this working. I was able to make this code work with a plane but when i create my own mesh tiles, it doesn't seem to work. I'm quite new to unity so i'm not sure why the plane would work and this created mesh would not as they both appear to be the same makeup. This code can be placed on an empty object.
Hex Tile Creation:
[ExecuteInEditMode]
//Checks if they exist at runtime
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]
public class TileMap : MonoBehaviour {
//basic hexagon mesh making
public Vector3[] vertices;
public int[] triangles;
public Vector3[] normals;
public Vector2[] uv;
// Use this for initialization
void Start () {
BuildMesh();
}
// Update is called once per frame
void BuildMesh() {
vertices = new Vector3[6];
triangles = new int[4 * 3]; // Hex is made up of 4 triangles with 3 verticies per triangle
normals = new Vector3[6]; //6 points that raise or lower
// Generate mesh data
float floorLevel = 0;
vertices = new Vector3[] //6 points in a hex
{
new Vector3(-1f , -.5f, floorLevel),
new Vector3(-1f, .5f, floorLevel),
new Vector3(0f, 1f, floorLevel),
new Vector3(1f, .5f, floorLevel),
new Vector3(1f, -.5f, floorLevel),
new Vector3(0f, -1f, floorLevel)
};
triangles = new int[] // 12 = 4 triangles * 3 verticies per triangle
{
1,5,0,
1,4,5,
1,2,4,
2,3,4
};
normals[0] = Vector3.up;
normals[1] = Vector3.up;
normals[2] = Vector3.up;
normals[3] = Vector3.up;
normals[4] = Vector3.up;
normals[5] = Vector3.up;
uv = new Vector2[]
{
new Vector2(0,0.25f),
new Vector2(0,0.75f),
new Vector2(0.5f,1),
new Vector2(1,0.75f),
new Vector2(1,0.25f),
new Vector2(0.5f,0),
};
// Create new mesh and populate data
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
mesh.uv = uv;
// Assign mesh to filter/renderer/collider
MeshFilter meshFilter = GetComponent<MeshFilter>();
MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
MeshCollider meshCollider = GetComponent<MeshCollider>();
meshFilter.mesh = mesh;
}
}
Color Hex Tile:
public class TileMouseOver : MonoBehaviour
{
public Color highlightColor;
Color normalColor;
void Start()
{
normalColor = GetComponent<Renderer>().material.color;
}
void Update()
{
Camera mainCamera = Camera.main;
Collider collider = GetComponent<Collider>();
Renderer renderer = GetComponent<Renderer>();
RaycastHit hitInfo;
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
if (collider.Raycast(ray, out hitInfo, Mathf.Infinity))
{
Debug.Log("In");
renderer.material.color = highlightColor;
}
else
{
renderer.material.color = normalColor;
}
}
}
Comment
Turns out that i didn't have the the mesh attached to the mesh collider.
// Create new mesh and populate data
$$anonymous$$esh mesh = new $$anonymous$$esh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.normals = normals;
mesh.uv = uv;
// Assign mesh to filter/renderer/collider
$$anonymous$$eshFilter meshFilter = GetComponent<$$anonymous$$eshFilter>();
$$anonymous$$eshRenderer meshRenderer = GetComponent<$$anonymous$$eshRenderer>();
$$anonymous$$eshCollider meshCollider = GetComponent<$$anonymous$$eshCollider>();
meshFilter.mesh = mesh;
------>meshCollider.shared$$anonymous$$esh = mesh; //allows raycast to recognize this
Your answer
![](https://koobas.hobune.stream/wayback/20220613195345im_/https://answers.unity.com/themes/thub/images/avi.jpg)