using UnityEngine; public class GrabStateMachine : MonoBehaviour { private bool grabbing = false; // 动画组件引用(需要在Inspector面板赋值,或通过代码自动获取) [Header("动画组件引用")] [SerializeField] private Animator anim; // 按键设置(可在Inspector面板修改,无需硬编码) [Header("控制按键")] [SerializeField] private KeyCode grabKey = KeyCode.G; /// /// 初始化 /// private void Start() { anim = GetComponent(); } 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); } }