You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RatAttack2D/Assets/Scripts/Interact.cs

56 lines
1.5 KiB

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<Interactable>().Interact();
}
}