diff options
| author | SmallFox <2806143047@qq.com> | 2026-01-31 12:31:28 +0800 |
|---|---|---|
| committer | SmallFox <2806143047@qq.com> | 2026-01-31 12:31:28 +0800 |
| commit | 64e326ff1720ceb5e4bdcb9a763f62de8ffbee4f (patch) | |
| tree | 7d2b321fb203b69e928641e5e1c49512bfcb694e /Assets/Scripts/GrabStateMachine.cs | |
| parent | 343a1aa731a85a8a866d0466d29931e5a9c8dfb0 (diff) | |
Drag
Diffstat (limited to 'Assets/Scripts/GrabStateMachine.cs')
| -rw-r--r-- | Assets/Scripts/GrabStateMachine.cs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/Assets/Scripts/GrabStateMachine.cs b/Assets/Scripts/GrabStateMachine.cs new file mode 100644 index 0000000..8242097 --- /dev/null +++ b/Assets/Scripts/GrabStateMachine.cs @@ -0,0 +1,47 @@ +using UnityEngine; + +public class GrabStateMachine : MonoBehaviour +{ + private bool grabbing = false; + + // 动画组件引用(需要在Inspector面板赋值,或通过代码自动获取) + [Header("动画组件引用")] + [SerializeField] private Animator anim; + + // 按键设置(可在Inspector面板修改,无需硬编码) + [Header("控制按键")] + [SerializeField] private KeyCode grabKey = KeyCode.G; + + /// <summary> + /// 初始化 + /// </summary> + private void Start() + { + anim = GetComponent<Animator>(); + } + + private void Update() + { + DetectGrabKeyInput(); + UpdateAnimatorState(); + } + + private void DetectGrabKeyInput() + { + if (Input.GetKey(grabKey)) + { + grabbing = true; + } + + if (Input.GetKeyUp(grabKey)) + { + grabbing = false; + } + } + + + private void UpdateAnimatorState() + { + anim.SetBool("grabbing", grabbing); + } +} |
