refactor (button) : change the script of the wall button, and ad it in the dev room

This commit is contained in:
Thibault Pouch
2026-03-08 11:27:25 +01:00
parent ecb2a911f3
commit cf523f77ec
3 changed files with 37240 additions and 72 deletions

View File

@@ -1,4 +1,3 @@
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;
@@ -7,23 +6,31 @@ using UnityEngine.InputSystem;
public class WallInteractButton : MonoBehaviour
{
[Header("Interaction")]
[Tooltip("Optional Input Action. If empty, fallback key will be used.")]
[SerializeField] private InputActionReference interactAction;
[Tooltip("Used only if no Input Action is assigned.")]
[SerializeField] private Key fallbackKey = Key.E;
[Tooltip("Key the player must press to interact (keyboard fallback).")]
[SerializeField] private Key interactKey = Key.E;
[Tooltip("If true, the button can only be triggered once.")]
[SerializeField] private bool oneShot = false;
[Header("Prompt")]
[SerializeField] private TMP_Text promptText;
[SerializeField] private string promptMessage = "Press E to interact";
[Header("Physical Press")]
[Tooltip("Transform that moves to simulate a physical press (optional).")]
[SerializeField] private Transform buttonMesh;
[Tooltip("How far the button moves when pressed.")]
[SerializeField] private float pressDepth = 0.05f;
[Tooltip("Speed of the press/release animation.")]
[SerializeField] private float pressSpeed = 10f;
[Header("Events")]
public UnityEvent OnInteract;
private bool m_playerInRange;
private bool m_hasInteracted;
private bool m_used;
private Vector3 m_buttonRestPos;
private Vector3 m_buttonPressedPos;
private bool m_isVisuallyPressed;
private void Reset()
{
@@ -31,79 +38,99 @@ public class WallInteractButton : MonoBehaviour
col.isTrigger = true;
}
private void OnEnable()
{
if (interactAction != null)
{
interactAction.action.Enable();
}
}
private void OnDisable()
{
if (interactAction != null)
{
interactAction.action.Disable();
}
}
private void Start()
{
UpdatePrompt(false);
if (buttonMesh != null)
{
m_buttonRestPos = buttonMesh.localPosition;
m_buttonPressedPos = m_buttonRestPos - buttonMesh.localRotation * Vector3.forward * pressDepth;
}
}
private void Update()
{
if (!m_playerInRange)
return;
if (oneShot && m_hasInteracted)
return;
if (WasInteractPressed())
if (m_playerInRange && Keyboard.current != null && Keyboard.current[interactKey].wasPressedThisFrame)
{
m_hasInteracted = true;
OnInteract?.Invoke();
TryInteract();
}
AnimateButton();
}
private void TryInteract()
{
if (oneShot && m_used)
return;
m_used = true;
m_isVisuallyPressed = true;
OnInteract?.Invoke();
if (!oneShot)
Invoke(nameof(ReleaseVisual), 0.15f);
}
private void ReleaseVisual()
{
m_isVisuallyPressed = false;
}
private void AnimateButton()
{
if (buttonMesh == null)
return;
Vector3 target = m_isVisuallyPressed ? m_buttonPressedPos : m_buttonRestPos;
buttonMesh.localPosition = Vector3.Lerp(buttonMesh.localPosition, target, Time.deltaTime * pressSpeed);
}
private void OnTriggerEnter(Collider other)
{
if (!IsPlayer(other))
return;
m_playerInRange = true;
UpdatePrompt(true);
if (IsPlayer(other))
m_playerInRange = true;
}
private void OnTriggerExit(Collider other)
{
if (!IsPlayer(other))
return;
m_playerInRange = false;
UpdatePrompt(false);
}
private bool WasInteractPressed()
{
if (interactAction != null)
return interactAction.action.WasPressedThisFrame() || interactAction.action.WasPerformedThisFrame();
return Keyboard.current != null && Keyboard.current[fallbackKey].wasPressedThisFrame;
if (IsPlayer(other))
m_playerInRange = false;
}
private bool IsPlayer(Collider other)
{
return other.CompareTag("Player") || other.GetComponentInParent<PlayerMovement>() != null;
if (other.CompareTag("Player"))
return true;
if (other.GetComponentInParent<PlayerMovement>() != null)
return true;
return false;
}
private void UpdatePrompt(bool visible)
#if UNITY_EDITOR
private void OnDrawGizmos()
{
if (promptText == null)
Collider col = GetComponent<Collider>();
if (col == null)
return;
promptText.text = promptMessage;
promptText.gameObject.SetActive(visible);
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.color = m_playerInRange
? new Color(0f, 1f, 0f, 0.25f)
: new Color(1f, 0.9f, 0f, 0.15f);
if (col is SphereCollider sphere)
{
Gizmos.DrawSphere(sphere.center, sphere.radius);
Gizmos.color = m_playerInRange ? Color.green : Color.yellow;
Gizmos.DrawWireSphere(sphere.center, sphere.radius);
}
else if (col is BoxCollider box)
{
Gizmos.DrawCube(box.center, box.size);
Gizmos.color = m_playerInRange ? Color.green : Color.yellow;
Gizmos.DrawWireCube(box.center, box.size);
}
}
#endif
}

File diff suppressed because one or more lines are too long