Game Object under Cursor 3D#
Input / Game Object under Cursor 3D
Can Read
Cannot Write
Description#
The Game Object with a 3D 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 |
| Use Triggers | Whether to check for Colliders marked as triggers or not |
| Camera | The Camera the ray is cast from |
Code#
using System;
using UnityEngine;
using UnityEngine.InputSystem;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Game Object under Cursor 3D")]
[Category("Input/Game Object under Cursor 3D")]
[Description("The Game Object with a 3D collider found under the Mouse cursor")]
[Image(typeof(IconCursor), ColorTheme.Type.Blue)]
[Parameter("Distance", "The maximum distance the ray should check")]
[Parameter("Layer Mask", "Filter to check objects only on specified layers")]
[Parameter("Use Triggers", "Whether to check for Colliders marked as triggers or not")]
[Parameter("Camera", "The Camera the ray is cast from")]
[Keywords("Mouse", "Pointer", "Pick", "Select", "Ray", "Cast", "Hover", "3D")]
[Serializable]
public class TypeGameObjectInputCursorPhysics3D : TypeGameObject
{
[SerializeField] private GetNumber m_Distance = new GetNumber(1000f);
[SerializeField] private LayerMask m_LayerMask = Physics.DefaultRaycastLayers;
[SerializeField] private GetBool m_UseTriggers = new GetBool(false);
[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());
QueryTriggerInteraction triggerInteraction = this.m_UseTriggers.Get(args)
? QueryTriggerInteraction.Collide
: QueryTriggerInteraction.Ignore;
bool isHit = Physics.Raycast(
ray,
out RaycastHit hit,
(float) this.m_Distance.Get(args),
this.m_LayerMask,
triggerInteraction
);
return isHit ? hit.collider.gameObject : null;
}
public override string ToString()
{
return "Game Object under Cursor 3D";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}