instantiate collision on mouseclick
Hello I'm making a simple game where blocks drop from the top to bottom along a grid. The grid is composed of prefab blocks listed under a 'clickable' layer.
I am trying to figure out how to store the coordinates for mousePosition.x/y and compare them to the coordinated of the surrounding blocks. The goal is to highlight to block closest to the click point.
More specifically in the example if the blue diamond is the clicked point, the yellow circle is instantiated and collects all the red points as collisions (Boxes B2, B3, and C2. It should then take the shortest distance between all of them (absolute value of subtracting the points) and only mark the lowest value as the target.
Here's what I've stumbled through so far but I'm not sure how to store those variables. For now I'm printing them in the debug log.
public class Highlight : MonoBehaviour {
public Transform clickPoint;
public float clickRange = .5f;
public LayerMask clickableLayers;
void Update()
{
if (Input.GetMouseButton(0))
{
Vector2 screenPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Debug.Log(Input.mousePosition.x);
Debug.Log(Input.mousePosition.y);
Collider2D[] hitSlots = Physics2D.OverlapCircleAll(clickPoint.position, clickRange, clickableLayers);
}
}
}