File size: 1,388 Bytes
dfdb6c1
 
 
 
 
 
 
 
 
 
 
1eb49ab
dfdb6c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c826805
 
 
 
 
 
 
dfdb6c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;

public class InteractionManager : MonoBehaviour
{
    public static InteractionManager instance;
    public InteractionUI ui;

    public Camera playerCam;
    public float dist = 5;

    private void Awake()
    {
        instance = this;
    }

    public void ShowUI(IInteractable interactable)
    {
        ui.gameObject.SetActive(true);
        ui.setUI(interactable.GetSprite(),interactable.GetHandle());

    }

    public void HideUI()
    {
        ui.gameObject.SetActive(false);
    }

    private void Update()
    {
        if(playerCam == default)
        {
            return;
        }

        // hide UI If we are holding an object
        if (PickUpScript.instance != null && PickUpScript.instance.heldObj != null)
        {
            HideUI();
            return;
        }

        RaycastHit hit = default;

        if (Physics.Raycast(playerCam.transform.position, playerCam.transform.forward, out hit, dist))
        {
            if(hit.transform.TryGetComponent(out IInteractable interactable))
            {
                ShowUI(interactable);
                if (interactable.CanInteract())
                {
                    interactable.interact();
                }

                return;
            }
        }

        HideUI();

    }
}