116 lines
3.7 KiB
C#
116 lines
3.7 KiB
C#
using UnityEngine;
|
|
using RaycastPro;
|
|
using RaycastPro.RaySensors;
|
|
using UnityEditor;
|
|
using System;
|
|
|
|
public class PlayerInteraction : MonoBehaviour
|
|
{
|
|
|
|
public BasicRay basicRay;
|
|
private bool isInteracting = false;
|
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start()
|
|
{
|
|
|
|
} // Update is called once per frame
|
|
void Update()
|
|
{
|
|
CheckForInteraction();
|
|
}
|
|
|
|
private void CheckForInteraction()
|
|
{
|
|
if (basicRay != null && basicRay.Hit.collider != null)
|
|
{
|
|
basicRay.Hit.collider.TryGetComponent(out IInteractable interactable);
|
|
if (interactable != null && interactable.CanInteract)
|
|
{
|
|
// Show interaction text
|
|
UIController.Instance.ShowGeneralMessage(interactable.InteractionText);
|
|
|
|
// Check for interaction input
|
|
if (Input.GetKeyDown(KeyCode.E) && !isInteracting)
|
|
{
|
|
isInteracting = true; // Prevent multiple interactions
|
|
UnlockMouse(); // Unlock mouse to allow interaction
|
|
interactable.Interact(gameObject);
|
|
Debug.Log($"Interacted with: {interactable.InteractionText}");
|
|
// Optionally, hide the message after interaction
|
|
UIController.Instance.HideGeneralMessage();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// If the item is not interactable, show its name
|
|
ShowItemNameOnly();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Hide message when not looking at anything
|
|
UIController.Instance.HideGeneralMessage();
|
|
}
|
|
} public void ShowName()
|
|
{
|
|
// This method can be called manually if needed
|
|
// The main interaction logic is now in CheckForInteraction()
|
|
CheckForInteraction();
|
|
}
|
|
|
|
private void ShowItemNameOnly()
|
|
{
|
|
//Debug.Log("Item Name: " + basicRay.Hit.collider.name);
|
|
string itemName = basicRay.Hit.collider.name;
|
|
UIController.Instance.ShowGeneralMessage(itemName);
|
|
}
|
|
|
|
public void UnlockMouse()
|
|
{
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
|
|
// Disable Player Controller to stop camera and movement
|
|
var firstPersonController = GetComponent<StarterAssets.FirstPersonController>();
|
|
if (firstPersonController != null)
|
|
{
|
|
firstPersonController.enabled = false;
|
|
}
|
|
|
|
// Also try to find any component with "Player" in the name
|
|
var allComponents = GetComponents<MonoBehaviour>();
|
|
foreach (var component in allComponents)
|
|
{
|
|
if (component.GetType().Name.ToLower().Contains("player") && component != this)
|
|
{
|
|
component.enabled = false;
|
|
}
|
|
}
|
|
}
|
|
public void LockMouse()
|
|
{
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
|
|
// Re-enable Player Controller to resume camera and movement
|
|
var firstPersonController = GetComponent<StarterAssets.FirstPersonController>();
|
|
if (firstPersonController != null)
|
|
{
|
|
firstPersonController.enabled = true;
|
|
}
|
|
|
|
// Also try to re-enable any component with "Player" in the name
|
|
var allComponents = GetComponents<MonoBehaviour>();
|
|
foreach (var component in allComponents)
|
|
{
|
|
if (component.GetType().Name.ToLower().Contains("player") && component != this)
|
|
{
|
|
component.enabled = true;
|
|
}
|
|
}
|
|
isInteracting = false; // Reset interaction state
|
|
Debug.Log("Mouse locked and PlayerController enabled.");
|
|
}
|
|
}
|