I’ve been following a youtube series on making a procedurally generated tile map. Everything has gone great so far until now. The videos were made in 2013 so I had to update my code to Unity 5 in order to work.
My problem is I generate the mesh with the texture on it. When I move the mouse over the mesh at all in game mode it dissapears. What I’m trying to do is have a 1x1 cube represent where the mouse is on the tile map.
I’m not sure what the problem could be or why it just dissapears.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(TileMap))]
public class TileMapMouse : MonoBehaviour {
Collider collider;
Renderer renderer;
TileMap _TileMap;
Vector3 currentTileCoord;
public Transform selectionCube;
void Start()
{
collider = GetComponent<Collider>();
renderer = GetComponent<Renderer>();
_TileMap = GetComponent<TileMap>();
selectionCube = GetComponent<Transform>();
}
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if( collider.Raycast( ray, out hitInfo, Mathf.Infinity))
{
int x = Mathf.FloorToInt( hitInfo.point.x / _TileMap.tileSize);
int z = Mathf.FloorToInt(hitInfo.point.z / _TileMap.tileSize);
currentTileCoord.x = x;
currentTileCoord.z = z;
selectionCube.transform.position = currentTileCoord * 5f;
}
else
{
}
}
}