miepzerino
2025-04-08 40ac185dc7a017d95771fe580c77eab20e663908
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// PlayerInteraction.cs - Handles player's interactions with objects
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.TextCore.Text;
 
public class PlayerInteraction : MonoBehaviour
{
    [SerializeField] private float checkRadius = 3f;
    [SerializeField] private LayerMask interactableMask;
    [SerializeField] public KeyCode interactKey = KeyCode.E;
 
    // UI references
    [SerializeField] private GameObject interactionPromptUI;
    [SerializeField] private TextMeshProUGUI promptText;
    [SerializeField] private float promptHeightOffset = 1.5f; // Height above the interactable
 
    private Interactable currentInteractable;
 
    private void Update()
    {
        // Check for interactable objects
        //CheckForInteractable();
 
        // Handle interaction input
        if (Input.GetKeyDown(interactKey) && currentInteractable != null && !GameManager.GameIsPaused)
        {
            currentInteractable.Interact();
        }
    }
    private void LateUpdate()
    {
        if (currentInteractable != null && interactionPromptUI.activeSelf)
        {
            // Update position every frame when active
            Vector3 targetPosition = currentInteractable.transform.position; // + Vector3.up * promptHeightOffset;
            interactionPromptUI.transform.position = targetPosition;
        }
    }
 
    public void SetCurrentInteractable(Interactable interactable)
    {
        currentInteractable = interactable;
        UpdateInteractionUI();
    }
 
    private void CheckForInteractable()
    {
        // Cast a sphere to detect interactable objects
        Collider[] colliders = Physics.OverlapSphere(transform.position, checkRadius, interactableMask);
        Debug.Log("Checking for interactables within radius: " + checkRadius);
        // Find closest interactable
        float closestDistance = checkRadius;
        Interactable closestInteractable = null;
 
        Debug.Log("Found " + colliders.Length + " colliders in range.");
        foreach (var collider in colliders)
        {
            Debug.Log("Collider detected: " + collider.gameObject.name);
            if (collider.TryGetComponent(out Interactable interactable))
            {
                float distance = Vector3.Distance(transform.position, interactable.transform.position);
                if (distance < closestDistance && interactable.IsInRange(transform))
                {
                    closestDistance = distance;
                    closestInteractable = interactable;
                }
            }
        }
 
        // Update current interactable
        if (closestInteractable != currentInteractable)
        {
            currentInteractable = closestInteractable;
            UpdateInteractionUI();
        }
    }
 
    private void UpdateInteractionUI()
    {
        if (currentInteractable != null)
        {
            // Update prompt text
            promptText.text = currentInteractable.GetPromptMessage();
 
            // Show the prompt
            interactionPromptUI.SetActive(true);
 
            // Position the prompt above the interactable
            Vector3 targetPosition = currentInteractable.transform.position + Vector3.up * promptHeightOffset;
            interactionPromptUI.transform.position = targetPosition;
        }
        else
        {
            interactionPromptUI.SetActive(false);
        }
    }
}