本文介绍了如何停止对角线运动 - Unity 2d?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在 Unity (2d) 中用于玩家移动的脚本.

This my script for player movement in Unity (2d).

当按下两个方向键时 - 而不是对角移动 - 我需要玩家在最近按下的方向上移动(如果松开,则是已经按下的方向)

When two direction keys are pressed - instead of moving diagonally - I need the player to move in the most recently pressed direction (and if that is released, the direction of the already held down)

if (!attacking)
{
    if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
    {
        //transform.Translate (new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f ));
        myRigidBody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * currentMoveSpeed, myRigidBody.velocity.y);
        PlayerMoving = true;
        lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
    }

    if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
    {
        //transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
        myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, Input.GetAxisRaw("Vertical") * currentMoveSpeed);
        PlayerMoving = true;
        lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
    }
}

推荐答案

以下是我的处理方式:当只有一个轴处于活动状态(水平或垂直)时,请记住那个方向.当两者都存在时,优先考虑不存在的那个.以下代码完全按照您的描述工作,但必须根据您的其他要求进行调整.

Here is how I would handle it: When only one axis is active (horizontal or vertical) remember that direction. When both are, prioritize the one that wasn't. The following code works exactly as you described but will have to be adapted to your other requirements.

void Update()
{
    float currentMoveSpeed = moveSpeed * Time.deltaTime;

    float horizontal = Input.GetAxisRaw("Horizontal");
    bool isMovingHorizontal = Mathf.Abs(horizontal) > 0.5f;

    float vertical = Input.GetAxisRaw("Vertical");
    bool isMovingVertical = Mathf.Abs(vertical) > 0.5f;

    PlayerMoving = true;

    if (isMovingVertical && isMovingHorizontal)
    {
        //moving in both directions, prioritize later
        if (wasMovingVertical)
        {
            myRigidBody.velocity = new Vector2(horizontal * currentMoveSpeed, 0);
            lastMove = new Vector2(horizontal, 0f);
        }
        else
        {
            myRigidBody.velocity = new Vector2(0, vertical * currentMoveSpeed);
            lastMove = new Vector2(0f, vertical);
        }
    }
    else if (isMovingHorizontal)
    {
        myRigidBody.velocity = new Vector2(horizontal * currentMoveSpeed, 0);
        wasMovingVertical = false;
        lastMove = new Vector2(horizontal, 0f);
    }
    else if (isMovingVertical)
    {
        myRigidBody.velocity = new Vector2(0, vertical * currentMoveSpeed);
        wasMovingVertical = true;
        lastMove = new Vector2(0f, vertical);
    }
    else
    {
        PlayerMoving = false;
        myRigidBody.velocity = Vector2.zero;
    }
}

示例结果(粉色线是 lastMove):

Example result (pink line is lastMove):

这篇关于如何停止对角线运动 - Unity 2d?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:37