Skip to content

Commit

Permalink
feat(behaviour): add interactable (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenira committed Mar 17, 2024
1 parent 5716fa2 commit 8b24847
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions plugin/src/objects/behaviours/Interactable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using PiUtils.Util;
using UnityEngine;

namespace PiUtils.Objects.Behaviours;

public class Interactable : MonoBehaviour
{
private static PluginLogger Logger = PluginLogger.GetLogger<Interactable>();

public Transform interactionTransform;
public float radius = 0.1f;
public event Action OnEnter;
public event Action OnExit;

public bool isHovered { get; private set; } = false;

private void Update()
{
if (interactionTransform == null)
{
return;
}

float distance = Vector3.Distance(interactionTransform.position, transform.position);
if (distance <= radius && !isHovered)
{
Logger.LogDebug($"Hovered {gameObject.name}");
isHovered = true;
OnEnter?.Invoke();
return;
}

if (distance > radius && isHovered)
{
Logger.LogDebug($"Unhovered {gameObject.name}");
isHovered = false;
OnExit?.Invoke();
}
}
}

0 comments on commit 8b24847

Please sign in to comment.