refactor (button) : change the script of the wall button, and ad it in the dev room
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
using TMPro;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Events;
|
using UnityEngine.Events;
|
||||||
using UnityEngine.InputSystem;
|
using UnityEngine.InputSystem;
|
||||||
@@ -7,23 +6,31 @@ using UnityEngine.InputSystem;
|
|||||||
public class WallInteractButton : MonoBehaviour
|
public class WallInteractButton : MonoBehaviour
|
||||||
{
|
{
|
||||||
[Header("Interaction")]
|
[Header("Interaction")]
|
||||||
[Tooltip("Optional Input Action. If empty, fallback key will be used.")]
|
[Tooltip("Key the player must press to interact (keyboard fallback).")]
|
||||||
[SerializeField] private InputActionReference interactAction;
|
[SerializeField] private Key interactKey = Key.E;
|
||||||
|
|
||||||
[Tooltip("Used only if no Input Action is assigned.")]
|
|
||||||
[SerializeField] private Key fallbackKey = Key.E;
|
|
||||||
|
|
||||||
|
[Tooltip("If true, the button can only be triggered once.")]
|
||||||
[SerializeField] private bool oneShot = false;
|
[SerializeField] private bool oneShot = false;
|
||||||
|
|
||||||
[Header("Prompt")]
|
[Header("Physical Press")]
|
||||||
[SerializeField] private TMP_Text promptText;
|
[Tooltip("Transform that moves to simulate a physical press (optional).")]
|
||||||
[SerializeField] private string promptMessage = "Press E to interact";
|
[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")]
|
[Header("Events")]
|
||||||
public UnityEvent OnInteract;
|
public UnityEvent OnInteract;
|
||||||
|
|
||||||
private bool m_playerInRange;
|
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()
|
private void Reset()
|
||||||
{
|
{
|
||||||
@@ -31,79 +38,99 @@ public class WallInteractButton : MonoBehaviour
|
|||||||
col.isTrigger = true;
|
col.isTrigger = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnEnable()
|
|
||||||
{
|
|
||||||
if (interactAction != null)
|
|
||||||
{
|
|
||||||
interactAction.action.Enable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnDisable()
|
|
||||||
{
|
|
||||||
if (interactAction != null)
|
|
||||||
{
|
|
||||||
interactAction.action.Disable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
UpdatePrompt(false);
|
if (buttonMesh != null)
|
||||||
|
{
|
||||||
|
m_buttonRestPos = buttonMesh.localPosition;
|
||||||
|
m_buttonPressedPos = m_buttonRestPos - buttonMesh.localRotation * Vector3.forward * pressDepth;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
if (!m_playerInRange)
|
if (m_playerInRange && Keyboard.current != null && Keyboard.current[interactKey].wasPressedThisFrame)
|
||||||
return;
|
|
||||||
|
|
||||||
if (oneShot && m_hasInteracted)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (WasInteractPressed())
|
|
||||||
{
|
{
|
||||||
m_hasInteracted = true;
|
TryInteract();
|
||||||
OnInteract?.Invoke();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
private void OnTriggerEnter(Collider other)
|
||||||
{
|
{
|
||||||
if (!IsPlayer(other))
|
if (IsPlayer(other))
|
||||||
return;
|
|
||||||
|
|
||||||
m_playerInRange = true;
|
m_playerInRange = true;
|
||||||
UpdatePrompt(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnTriggerExit(Collider other)
|
private void OnTriggerExit(Collider other)
|
||||||
{
|
{
|
||||||
if (!IsPlayer(other))
|
if (IsPlayer(other))
|
||||||
return;
|
|
||||||
|
|
||||||
m_playerInRange = false;
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsPlayer(Collider other)
|
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;
|
return;
|
||||||
|
|
||||||
promptText.text = promptMessage;
|
Gizmos.matrix = transform.localToWorldMatrix;
|
||||||
promptText.gameObject.SetActive(visible);
|
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
@@ -69,11 +69,16 @@
|
|||||||
{
|
{
|
||||||
"type": "System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
|
"type": "System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
|
||||||
"key": "ShapeBuilder.ActiveShapeIndex",
|
"key": "ShapeBuilder.ActiveShapeIndex",
|
||||||
"value": "{\"m_Value\":6}"
|
"value": "{\"m_Value\":3}"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "UnityEngine.Vector3, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
|
"type": "UnityEngine.Vector3, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
|
||||||
"key": "ShapeBuilder.LastSize.Cube",
|
"key": "ShapeBuilder.LastSize.Cube",
|
||||||
|
"value": "{\"m_Value\":{\"x\":0.10000000149011612,\"y\":2.0,\"z\":2.0}}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "UnityEngine.Vector3, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
|
||||||
|
"key": "ShapeBuilder.LastSize.Sphere",
|
||||||
"value": "{\"m_Value\":{\"x\":0.5,\"y\":0.5,\"z\":0.5}}"
|
"value": "{\"m_Value\":{\"x\":0.5,\"y\":0.5,\"z\":0.5}}"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -81,16 +86,31 @@
|
|||||||
"key": "ShapeBuilder.LastRotation.Cube",
|
"key": "ShapeBuilder.LastRotation.Cube",
|
||||||
"value": "{\"m_Value\":{\"x\":0.0,\"y\":0.0,\"z\":0.0,\"w\":1.0}}"
|
"value": "{\"m_Value\":{\"x\":0.0,\"y\":0.0,\"z\":0.0,\"w\":1.0}}"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "UnityEngine.Quaternion, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
|
||||||
|
"key": "ShapeBuilder.LastRotation.Sphere",
|
||||||
|
"value": "{\"m_Value\":{\"x\":0.0,\"y\":0.0,\"z\":0.0,\"w\":1.0}}"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "UnityEngine.ProBuilder.PivotLocation, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
|
"type": "UnityEngine.ProBuilder.PivotLocation, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
|
||||||
"key": "ShapeBuilder.PivotLocation.Cube",
|
"key": "ShapeBuilder.PivotLocation.Cube",
|
||||||
"value": "{\"m_Value\":0}"
|
"value": "{\"m_Value\":0}"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "UnityEngine.ProBuilder.PivotLocation, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
|
||||||
|
"key": "ShapeBuilder.PivotLocation.Sphere",
|
||||||
|
"value": "{\"m_Value\":0}"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "UnityEngine.ProBuilder.Shapes.Shape, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
|
"type": "UnityEngine.ProBuilder.Shapes.Shape, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
|
||||||
"key": "ShapeBuilder.Cube",
|
"key": "ShapeBuilder.Cube",
|
||||||
"value": "{}"
|
"value": "{}"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "UnityEngine.ProBuilder.Shapes.Shape, Unity.ProBuilder, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
|
||||||
|
"key": "ShapeBuilder.Sphere",
|
||||||
|
"value": "{}"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "UnityEngine.Rendering.ShadowCastingMode, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
|
"type": "UnityEngine.Rendering.ShadowCastingMode, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null",
|
||||||
"key": "mesh.shadowCastingMode",
|
"key": "mesh.shadowCastingMode",
|
||||||
|
|||||||
Reference in New Issue
Block a user