summaryrefslogtreecommitdiff
path: root/Assets/Scripts/GrabStateMachine.cs
blob: 82420976d16f8dbbf633e19c2c5ddc5ded36f6ca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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);
    }
}