C# 如何停止对角线运动-Unity 2d?

C# 如何停止对角线运动-Unity 2d?,c#,unity3d,C#,Unity3d,这是我在Unity(2d)中的玩家移动脚本 当按下两个方向键时-而不是对角移动-我需要播放器沿最近按下的方向移动(如果释放,则为已按下的方向) if(!攻击) { if(Input.GetAxisRaw(“水平”)>0.5f | | Input.GetAxisRaw(“水平”)0.5f | | Input.GetAxisRaw(“垂直”)

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

当按下两个方向键时-而不是对角移动-我需要播放器沿最近按下的方向移动(如果释放,则为已按下的方向)

if(!攻击)
{
if(Input.GetAxisRaw(“水平”)>0.5f | | Input.GetAxisRaw(“水平”)<-0.5f)
{
//transform.Translate(新矢量3(Input.GetAxisRaw(“水平”)*moveSpeed*Time.deltaTime,0f,0f));
myRigidBody.velocity=新矢量2(Input.GetAxisRaw(“水平”)*currentMoveSpeed,myRigidBody.velocity.y);
PlayerMoving=true;
lastMove=newvector2(Input.GetAxisRaw(“水平”),0f);
}
如果(Input.GetAxisRaw(“垂直”)>0.5f | | Input.GetAxisRaw(“垂直”)<-0.5f)
{
//transform.Translate(新矢量3(0f,Input.GetAxisRaw(“垂直”)*moveSpeed*Time.deltaTime,0f));
myRigidBody.velocity=新矢量2(myRigidBody.velocity.x,Input.GetAxisRaw(“垂直”)*currentMoveSpeed);
PlayerMoving=true;
lastMove=newvector2(0f,Input.GetAxisRaw(“垂直”);
}
}

我将如何处理它:当只有一个轴处于活动状态(水平或垂直)时,请记住该方向。当两者都存在时,优先考虑不存在的一个。以下代码的工作原理与您描述的完全相同,但必须根据您的其他需求进行调整

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
):
我将如何处理它:当只有一个轴处于活动状态(水平或垂直)时,请记住该方向。当两者都存在时,优先考虑不存在的一个。以下代码的工作原理与您描述的完全相同,但必须根据您的其他需求进行调整

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
):

添加两个私有变量,记住前面的值并进行比较?你是在寻找如何处理这个问题的基本思路,还是在编码上有困难?添加几个私有变量,记住前面的值并进行比较?您是在寻找如何处理此问题的基本思路,还是在编写代码时遇到了问题?