首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >统一-人物不会停止移动,即使没有按下任何东西

统一-人物不会停止移动,即使没有按下任何东西
EN

Stack Overflow用户
提问于 2021-09-26 12:47:13
回答 1查看 57关注 0票数 0

好的,下面是代码:

代码语言:javascript
复制
// Variable to store character animator component
Animator animator;

// Variables to store optimized setter/getter parameter IDs
int isWalkingHash;
int isRunningHash;

// Variable to store the instance of the PlayerInput
PlayerInput input;

// Variables to store player input values
Vector2 currentMovement;
bool movementPressed;
bool runPressed;

// Awake is called when the script instance is being loaded
void Awake() 
{
     input = new PlayerInput();

    // Set the player input values using listeners
     input.CharacterControls.Movement.performed += ctx => {
        currentMovement = ctx.ReadValue<Vector2>();
        movementPressed = currentMovement.x != 0 || currentMovement.y != 0;
     };  
     
     input.CharacterControls.Run.performed += ctx => runPressed = ctx.ReadValueAsButton();
}

// Start is called before the first frame update
void Start()
{
    // Set the animator reference
    animator = GetComponent<Animator>();

    // Set ID references
    isWalkingHash = Animator.StringToHash("isWalking");
    isRunningHash = Animator.StringToHash("isRunning");
}

// Update is called once per frame
void Update()
{
    handleMovement();   
}

void handleMovement()
{
    // Get parameter values from animator
    bool isRunning = animator.GetBool(isRunningHash);
    bool isWalking = animator.GetBool(isWalkingHash);

    // Start walking if movement pressed is true and not already walking
    if (movementPressed && !isWalking) {
        animator.SetBool(isWalkingHash, true);
    }

    // Stop walking if movementPressed is false and not already walking
    if (!movementPressed && isWalking) {
        animator.SetBool(isWalkingHash, false);
    }

    // Start running if movement pressed and run pressed is true and not already running
    if ((movementPressed && runPressed) && isRunning) {
        animator.SetBool(isRunningHash, true);
    }

    // Stop running if movement pressed or run pressed is false and not currently running
    if ((!movementPressed || !runPressed) && isRunning) {
        animator.SetBool(isRunningHash, false);
    }
}

void OnEnable()
{
    //Enable the character control action map
    input.CharacterControls.Enable();
}

void OnDisable()
{
    //Disable the character control action map
    input.CharacterControls.Disable();
} 

我一开始使用的是UnityEngine.InputSystem。但是移动效果很好,就在我松开WASD的时候,它仍然在最后一个按下的方向上移动。我真的不知道这里有什么问题。我刚刚开始,所以我不知道最简单的问题的答案。很抱歉打扰你。提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2021-09-26 13:38:31

在下面的代码块中

代码语言:javascript
复制
// Stop walking if movementPressed is false and not already walking
    if (!movementPressed && isWalking) {
        animator.SetBool(isWalkingHash, false);
    }

你说你正在检查“还没有走”。它不会是'!isWalking‘作为if条件吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69335066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档