im trying to have a constant raycast to detect what tile the mouse is hovered over to activate a child gameobject which displays a highlight graphic. The ray works but it activates every tiles highlight instead of just the tile touched by the mouse, how do i make it activate a specific tiles highlight? It needs to be a ray because the detection needs to pierce through multiple gameobjects whereas OnMouseEnter only detects the top one.
code to create the ray
public RaycastHit2D tileRay;
public LayerMask tile;
void Update()
{
tileRay = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, tile);
}
and this is what i believe is the problem code which is in a seperate script attatched to the tile prefab
[SerializeField] private GameObject highlight;
public GameObject mainCam;
void Start()
{
mainCam = GameObject.Find("Main Camera");
}
void Update()
{
RaycastHit2D tileRay = mainCam.GetComponent<CameraController>().tileRay;
if (tileRay.collider != null)
{
highlight.SetActive(true);
}
}
working with little unity knowledge and cant find other sources that apply to my problem, thanks!