You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CrowdControl/Assets/Plugins/Animancer/Examples/02 Fine Control/02 Doors/ClickToInteract.cs

46 lines
1.6 KiB
C#

3 months ago
// Animancer // Copyright 2020 Kybernetik //
using UnityEngine;
namespace Animancer.Examples.FineControl
{
/// <summary>An object that can be interacted with.</summary>
public interface IInteractable
{
/************************************************************************************************************************/
void Interact();
/************************************************************************************************************************/
}
/// <summary>
/// Attempts to interact with whatever <see cref="IInteractable"/> the cursor is pointing at when the user clicks
/// the mouse.
/// </summary>
[AddComponentMenu(Strings.MenuPrefix + "Examples/Fine Control - Click To Interact")]
[HelpURL(Strings.APIDocumentationURL + ".Examples.FineControl/ClickToInteract")]
public sealed class ClickToInteract : MonoBehaviour
{
/************************************************************************************************************************/
private void Update()
{
if (!Input.GetMouseButtonDown(0))
return;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit raycastHit;
if (Physics.Raycast(ray, out raycastHit))
{
var interactable = raycastHit.collider.GetComponentInParent<IInteractable>();
if (interactable != null)
interactable.Interact();
}
}
/************************************************************************************************************************/
}
}