Skip to content
Snippets Groups Projects
InteractCursor.cs 1015 B
Newer Older
  • Learn to ignore specific revisions
  • Josiane Pos's avatar
    Josiane Pos committed
    using System;
    using Interactable;
    using UnityEngine;
    using UnityEngine.InputSystem;
    
    public class InteractCursor : MonoBehaviour
    {
        private IInteractable m_currentInteractable;
        private InputAction m_Interact;
    
        private void Start()
        {
            m_Interact = InputSystem.actions.FindAction("Interact");
        }
    
        private void Update()
        {
            if (m_Interact.WasPerformedThisFrame())
            {
                m_currentInteractable?.OnInteract();
            }
        }
    
        private void OnTriggerEnter(Collider other)
        {
            Debug.Log("Enter");
            
            if (other.TryGetComponent(out SpriteRenderer sprite))
            {
                sprite.color = Color.blue;
            }
            if (other.TryGetComponent(out IInteractable interactable))
            {
                m_currentInteractable = interactable;
            }
        }
    
        private void OnTriggerExit(Collider other)
        {
            if (other.TryGetComponent(out SpriteRenderer sprite))
            {
                sprite.color = Color.white;
            }
        }
    }