using DG.Tweening; using UnityEngine; public class ReleasableButton : MonoBehaviour { public delegate void OnButtonPressedDelegate(); public event OnButtonPressedDelegate OnButtonPressed; public Transform movableParts; public float moveDuration = 0.25f; public float moveAmount = 0.05f; private float upPositionY; private float downPositionY; private bool isButtonDown; private void Awake() { upPositionY = movableParts.localPosition.y; downPositionY = movableParts.localPosition.y - moveAmount; isButtonDown = false; } // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } private void OnTriggerEnter(Collider collider) { if (!isButtonDown && collider.gameObject.tag.EndsWith("Hand")) { Debug.Log("collided with: " + collider.gameObject.name); Activate(); OnButtonPressed?.Invoke(); } } private void OnTriggerExit(Collider collider) { if (isButtonDown && collider.gameObject.tag.EndsWith("Hand")) { Debug.Log("collider exited: " + collider.gameObject.name); Deactivate(); } } private void Activate() { movableParts.DOLocalMoveY(downPositionY, moveDuration); isButtonDown = true; AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.Click, gameObject); } private void Deactivate() { movableParts.DOLocalMoveY(upPositionY, moveDuration); isButtonDown = false; AudioManager.Instance.PlayAttachedInstance(FMODEvents.Instance.Click, gameObject); } }