/* * author: ryan lin, mark joshwel * date: 11/8/2024 * description: player interaction behaviour */ using UnityEngine; /// /// player interaction behaviour /// public class Player : MonoBehaviour { /// /// the position of the player /// [SerializeField] private Transform playerPosition; /// /// the maximum distance the player can interact with objects /// [SerializeField] private float interactableDistance; /// /// the layers the raycast should interact with /// public LayerMask raycastLayers; // /// // /// the current interactable object the player is looking at // /// // private CommonInteractable _currentInteractable; /// /// game manager instance /// private GameManager _game; /// /// whether the player is looking at an interactable object /// private bool _raycast; /// /// the raycast hit information /// public RaycastHit Hit; /// /// initialisation function /// private void Start() { _game = GameManager.Instance; } /// /// raycast performer for interactable objects /// private void Update() { _raycast = Physics.Raycast( playerPosition.position, playerPosition.TransformDirection(Vector3.forward), out Hit, interactableDistance, raycastLayers ); Debug.DrawRay( playerPosition.position, playerPosition.TransformDirection(Vector3.forward) * interactableDistance, Color.green ); if (!_raycast) return; // show an interaction prompt if we're looking at an interactable object var prompt = Hit.collider.GetComponent()?.interactionPrompt; if (prompt != "") Debug.Log(prompt); } /// /// handles the action when the player interacts with an object /// private void OnAction() { if (!_raycast) return; Hit.collider.GetComponent()?.Interact(); // _currentInteractable?.Interact(); } /// /// function called by the input system when escape is paused /// public void OnPause() { Debug.Log("escape pressed"); _game.SetDisplayState(_game.Paused ? GameManager.DisplayState.Game : GameManager.DisplayState.OverlayPauseMenu); } }