You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity方向按钮点击后球体无法正常移动的技术问题咨询

问题排查与修复方案

核心错误点

  • FixedUpdate中左右向量完全搞反:当isLeft触发时,代码错误使用了rightVector;isRight触发时用了leftVector,这直接导致点击Right按钮后球向左移动。
  • Left按钮的移动逻辑因向量错误失效:点击Left按钮时,代码给球添加了leftVector,但后续FixedUpdate里isLeft对应的是rightVector移动——如果leftVector和rightVector是相反方向(比如leftVector = -rightVector),两次移动会相互抵消,看起来球完全不动。

修正后的代码

private void FixedUpdate()
{
    if (isUp)
    {
        ball.transform.position += upVector * Time.fixedDeltaTime;
    }
    else if (isDown)
    {
        ball.transform.position += downVector * Time.fixedDeltaTime;
    }
    else if (isLeft)
    {
        // 修正:isLeft时使用leftVector
        ball.transform.position += leftVector * Time.fixedDeltaTime;
    }
    else if (isRight)
        // 修正:isRight时使用rightVector
        ball.transform.position += rightVector * Time.fixedDeltaTime;
}

public void MouseDown()
{
    isUp = isDown = isLeft = isRight = false;

    if (this.name == "Up")
    {
        ball.transform.position += upVector;
        isUp = true;
    }
    else if (this.name == "Down")
    {
        ball.transform.position += downVector;
        isDown = true;
    }
    else if (this.name == "Right")
    {
        ball.transform.position += rightVector;
        isRight = true;
    }
    else if (this.name == "Left")
    {
        ball.transform.position += leftVector;
        isLeft = true;
    }
}

上下方向异常的额外排查

如果上下按钮也有异常,检查upVector和downVector的定义:通常downVector应该是-upVector(比如upVector = Vector3.up,downVector = Vector3.down),确认向量值是否和预期的方向一致。

内容的提问来源于stack exchange,提问作者panly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 20:25:30