using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; public class Interact : MonoBehaviour { public GameObject interactImage; private Inputs Input; private InputAction Inter; private void Awake() { Input = new Inputs(); } private void OnEnable() { Inter = Input.Movement.Interact; Inter.Enable(); } private void OnDisable() { Inter.Disable(); } private void OnTriggerEnter2D(Collider2D other) { if(other.CompareTag("Interactable")) { Inter.Enable(); Inter.performed += _ => interact(other); interactImage.transform.position = other.transform.position + new Vector3(0, other.bounds.extents.y + 3, 0); interactImage.SetActive(true); } } private void OnTriggerStay2D(Collider2D other) { if(other.CompareTag("Interactable")) { interactImage.transform.position = other.transform.position + new Vector3(0, other.bounds.extents.y + 3, 0); } } private void OnTriggerExit2D(Collider2D other) { if(other.CompareTag("Interactable")) { Inter.Disable(); interactImage.SetActive(false); } } private void interact(Collider2D other) { Inter.Disable(); interactImage.SetActive(false); other.GetComponent().Interact(); } }