Commit 1
This commit is contained in:
76
Assets/Scripts/Debug/PlayerDebugProvider.cs
Normal file
76
Assets/Scripts/Debug/PlayerDebugProvider.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace OnlyScove.Scripts
|
||||
{
|
||||
public class PlayerDebugProvider : MonoBehaviour
|
||||
{
|
||||
[Header("References")]
|
||||
[SerializeField] private PlayerStateMachine stateMachine;
|
||||
[Tooltip("Kéo cái Canvas (World Space) bạn đã thiết kế vào đây")]
|
||||
[SerializeField] private GameObject debugCanvas;
|
||||
|
||||
[Header("Follow Settings")]
|
||||
[SerializeField] private Vector3 followOffset = new Vector3(1.5f, 2f, 0f);
|
||||
[SerializeField] private float smoothTime = 0.15f;
|
||||
[SerializeField] private bool lookAtCamera = true;
|
||||
|
||||
// Các thuộc tính Public để bạn truy cập từ Script UI của bạn
|
||||
public string CurrentState => stateMachine != null ? stateMachine.CurrentStateName : "N/A";
|
||||
public string GroundedStatus => (stateMachine != null && stateMachine.IsGrounded) ? "YES" : "NO";
|
||||
public float HorizontalSpeed => stateMachine != null ? new Vector3(stateMachine.Controller.velocity.x, 0, stateMachine.Controller.velocity.z).magnitude : 0f;
|
||||
public float VerticalSpeed => stateMachine != null ? stateMachine.VelocityY : 0f;
|
||||
public Vector2 MoveInput => stateMachine != null ? stateMachine.Input.MoveInput : Vector2.zero;
|
||||
public bool IsSprinting => stateMachine != null ? stateMachine.Input.IsSprintHeld : false;
|
||||
public string TargetInteractable => stateMachine != null ? (stateMachine.GetInteractable()?.InteractionPrompt ?? "None") : "N/A";
|
||||
|
||||
private Vector3 currentVelocity;
|
||||
private Transform cameraTransform;
|
||||
private bool isVisible = true;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (stateMachine == null) stateMachine = GetComponent<PlayerStateMachine>();
|
||||
cameraTransform = Camera.main?.transform;
|
||||
|
||||
if (debugCanvas != null) debugCanvas.SetActive(isVisible);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Toggle Visibility (Ctrl + Shift + B) sử dụng New Input System
|
||||
if (Keyboard.current != null)
|
||||
{
|
||||
bool ctrl = Keyboard.current.leftCtrlKey.isPressed || Keyboard.current.rightCtrlKey.isPressed;
|
||||
bool shift = Keyboard.current.leftShiftKey.isPressed || Keyboard.current.rightShiftKey.isPressed;
|
||||
bool bDown = Keyboard.current.bKey.wasPressedThisFrame;
|
||||
|
||||
if (ctrl && shift && bDown)
|
||||
{
|
||||
isVisible = !isVisible;
|
||||
if (debugCanvas != null) debugCanvas.SetActive(isVisible);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (!isVisible || debugCanvas == null) return;
|
||||
|
||||
// 1. Damping Follow: UI đuổi theo Player
|
||||
Vector3 targetPos = transform.position + followOffset;
|
||||
debugCanvas.transform.position = Vector3.SmoothDamp(
|
||||
debugCanvas.transform.position,
|
||||
targetPos,
|
||||
ref currentVelocity,
|
||||
smoothTime
|
||||
);
|
||||
|
||||
// 2. Billboard: Luôn nhìn về Camera
|
||||
if (lookAtCamera && cameraTransform != null)
|
||||
{
|
||||
debugCanvas.transform.LookAt(debugCanvas.transform.position + cameraTransform.forward);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Debug/PlayerDebugProvider.cs.meta
Normal file
2
Assets/Scripts/Debug/PlayerDebugProvider.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bf6aff0b7e11d41439ac80f4963a0795
|
||||
Reference in New Issue
Block a user