Mouse Raycast Point#
Input / Mouse Raycast Point
Can Read
Cannot Write
Description#
Returns the point where a ray cast from the Mouse hits a 3D collider
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("Mouse Raycast Point")]
[Category("Input/Mouse Raycast Point")]
[Description("Returns the point where a ray cast from the Mouse hits a 3D collider")]
[Image(typeof(IconCursor), ColorTheme.Type.Blue, typeof(BadgePhysics))]
[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("Cursor", "Screen", "Ray", "Cast", "Hit", "Physics", "3D")]
[Serializable]
public class TypeVector3InputMouseRaycastPoint : TypeVector3
{
[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 Vector3 Get(Args args)
{
Mouse mouse = Mouse.current;
if (mouse == null) return default;
Camera camera = this.m_Camera.Get<Camera>(args);
if (camera == null) return default;
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.point : default;
}
public override string ToString()
{
return "Mouse Raycast Point";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}