---
tags:
    - "Mouse"
    - "Pointer"
    - "Pick"
    - "Select"
    - "Ray"
    - "Cast"
    - "Hover"
    - "2D"
---

# Game Object under Cursor 2D

[Input](index.md) / **Game Object under Cursor 2D**

<div class="grid cards" markdown>

* :green_circle: Can Read

* :red_circle: Cannot Write

</div>

## Description

The Game Object with a 2D collider found under the Mouse cursor

## Parameters

| Name | Description |
| :--- | :---------- |
| Distance | The maximum distance the ray should check |
| Layer Mask | Filter to check objects only on specified layers |
| Camera | The Camera the ray is cast from |

## Code

```cs
using System;
using UnityEngine;
using UnityEngine.InputSystem;
using VisualScript.Icons;

namespace VisualScript.Runtime
{
    [Get]

    [Title("Game Object under Cursor 2D")]
    [Category("Input/Game Object under Cursor 2D")]

    [Description("The Game Object with a 2D collider found under the Mouse cursor")]
    [Image(typeof(IconCursor), ColorTheme.Type.Blue, typeof(Badge2D))]

    [Parameter("Distance", "The maximum distance the ray should check")]
    [Parameter("Layer Mask", "Filter to check objects only on specified layers")]
    [Parameter("Camera", "The Camera the ray is cast from")]

    [Keywords("Mouse", "Pointer", "Pick", "Select", "Ray", "Cast", "Hover", "2D")]

    [Serializable]
    public class TypeGameObjectInputCursorPhysics2D : TypeGameObject
    {
        [SerializeField] private GetNumber m_Distance = new GetNumber(1000f);
        [SerializeField] private LayerMask m_LayerMask = Physics2D.DefaultRaycastLayers;

        [SerializeField]
        private GetGameObject m_Camera = new GetGameObject(new TypeGameObjectCameraMain());

        public override GameObject Get(Args args)
        {
            Mouse mouse = Mouse.current;
            if (mouse == null) return null;

            Camera camera = this.m_Camera.Get<Camera>(args);
            if (camera == null) return null;

            Ray ray = camera.ScreenPointToRay(mouse.position.ReadValue());

            RaycastHit2D hit = Physics2D.GetRayIntersection(
                ray,
                (float) this.m_Distance.Get(args),
                this.m_LayerMask
            );

            return hit.collider != null ? hit.collider.gameObject : null;
        }

        public override string ToString()
        {
            return "Game Object under Cursor 2D";
        }

        protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
    }
}

```

