diff options
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); + } +} |
