如何在 Unity InputSystem 中保留输入

问题描述 投票:0回答:0

我想在玩家移动时执行翻滚,只要他们按住移动键,就可以让他们继续朝那个方向移动。但是由于Unity的InputSystem的Action函数是在InputValue改变的时候调用的,所以在滚动前按住键会出现问题,导致Action函数无法调用

using UnityEngine;
using UnityEngine.InputSystem;

public class Will : MonoBehaviour
{
    public enum WillState
    {
        IDLE,
        RUN,
        ROLL
    }

    private Rigidbody2D _rigid;
    private Animator _anim;

    public WillState _currentState;
    public Vector2 _moveInput;
    public float Horizontal { get; private set; }
    public float Vertical { get; private set; }

    private float _moveSpeed = 1.0f;
    private float _rollDistance = 1.4f;

    public bool _canMove = true;

    private void Awake()
    {
        _rigid = GetComponent<Rigidbody2D>();
        _anim = GetComponent<Animator>();
    }

    private void Update()
    {
        switch (_currentState)
        {
            case WillState.IDLE:
                _anim.Play("Idle");
                break;
            case WillState.RUN:
                _anim.SetFloat("MoveX", _moveInput.x);
                _anim.SetFloat("MoveY", _moveInput.y);
                _anim.Play("Run");
                break;
            case WillState.ROLL:
                _anim.Play("Roll");
                break;
        }
    }

    private void FixedUpdate()
    {
        switch (_currentState)
        {
            case WillState.IDLE:
                break;
            case WillState.RUN:
                _rigid.MovePosition(_rigid.position + _moveInput * _moveSpeed * Time.fixedDeltaTime);
                break;
            case WillState.ROLL:
                _rigid.MovePosition(_rigid.position + _moveInput * _rollDistance * Time.fixedDeltaTime);
                break;
        }
    }

    void OnMove(InputValue value)
    {
        if (_canMove)
        {
            _moveInput = value.Get<Vector2>();
            if (_moveInput != Vector2.zero)
            {
                ChangeState(WillState.RUN);
            }
            else
            {
                ChangeState(WillState.IDLE);
            }
        }
    }

    void OnRoll()
    {
        _canMove = false;
        ChangeState(WillState.ROLL);
    }

    void RollFinished()
    {
        _canMove = true;
        ChangeState(WillState.IDLE);
    }

    void ChangeState(WillState newState)
    {
        _currentState = newState;
    }

}

看了很多关于Unity InputSystem的视频和手册,但是我想不出一个好的解决方案

unity3d
© www.soinside.com 2019 - 2024. All rights reserved.