Untitled

 avatar
unknown
plain_text
25 days ago
933 B
3
Indexable
using UnityEngine;

public class ClickManager : MonoBehaviour
{
    [SerializeField] private Vector2 detectionSize = new Vector2(1f, 1f); // Size of the clickable area

    private Vector2 lastMousePosition;

    void Update()
    {
        if (Input.GetMouseButtonDown(0)) // Check for left mouse click
        {
            lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            Collider2D hit = Physics2D.OverlapPoint(lastMousePosition);

            if (hit != null)
            {
                Debug.Log("Clicked object: " + hit.gameObject.name);
            }
            else
            {
                Debug.Log("No object detected!");
            }
        }
    }

    private void OnDrawGizmos()
    {
        if (Camera.main == null) return; 

        Gizmos.color = Color.blue;
        Gizmos.DrawWireCube(lastMousePosition, detectionSize);
    }
}
Leave a Comment