【问题标题】:Disabling diagonal movement in Unity 5 2D? (using C#)在 Unity 5 2D 中禁用对角线移动? (使用 C#)
【发布时间】:2023-03-14 09:28:01
【问题描述】:

或者我应该说,“有效地禁用对角线运动”。

网上有很多关于这个的Q/As,但我一直遇到同样的问题:水平移动时(例如向左),我可以覆盖当前方向并开始垂直移动(通过向上推),即我想要的是。但反过来就不行了!垂直移动不能覆盖水平移动。

void Update () {

         float h = Input.GetAxis("Horizontal");
         float v = Input.GetAxis("Vertical");
          ManageMovement(h, v); 
}    

void ManageMovement(float horizontal,float vertical) {

        if (vertical != 0f) {
                horizontal = 0f;
                Vector3 movement = new Vector3 (horizontal, vertical, 0);
                GetComponent<Rigidbody2D> ().velocity = movement * speed;
                return;
            } 

        if (horizontal != 0f) {
            vertical = 0f;
            Vector3 movement = new Vector3 (horizontal, vertical, 0);
            GetComponent<Rigidbody2D> ().velocity = movement * speed;
            return;

        } else {
            Vector3 noMovement = new Vector3 (0, 0, 0);
            GetComponent<Rigidbody2D> ().velocity = noMovement;
        }
    }

如果我颠倒这些 if() 语句的顺序,它就会颠倒问题。所以,这是一个线索。但我不是一个伟大的侦探。我希望得到一些帮助!

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    尝试在 ManageMovement 方法中添加 else 语句:

    void ManageMovement(float horizontal,float vertical) {
    
        if (vertical != 0f) {
                horizontal = 0f;
                Vector3 movement = new Vector3 (horizontal, vertical, 0);
                GetComponent<Rigidbody2D> ().velocity = movement * speed;
                return;
            } 
    
        else if (horizontal != 0f) {
            vertical = 0f;
            Vector3 movement = new Vector3 (horizontal, vertical, 0);
            GetComponent<Rigidbody2D> ().velocity = movement * speed;
            return;
    
        } else {
            Vector3 noMovement = new Vector3 (0, 0, 0);
            GetComponent<Rigidbody2D> ().velocity = noMovement;
        }
    }
    

    【讨论】:

    • 嗨,大人,我在方法中添加了 else,但它似乎没有改变任何东西。垂直移动仍然占主导地位。
    【解决方案2】:

    使用 Input。GetAxisRaw 而不是 GetAxis。

    GetAxis 返回一个从 -1 到 1 的浮点数。它返回 0 的速度取决于轴设置。如果将重力设置为非常高的数字,它将更快地返回到 0。如果您将敏感度设置为非常高的数字,它会更快地变为 -1 或 1。

    因此,根据这些设置,您的函数 ManageMovement 将被多次调用,水平和垂直的值会逐渐变化。

    随着时间的推移输入可能如下所示:

    Update #1: ManageMovement(0.2, 1.0)
    Update #2: ManageMovement(0.3, 0.9)
    ...
    Update #N: ManageMovement(1.0, 0.0)
    

    因此,当您检查是否垂直!= 0 时,它将保持非零,直到它实际达到 0,然后对于之前的所有更新,您将水平设置为 0。

    GetAxisRaw 没有以这种方式平滑。

    【讨论】:

    • 天哪!是的!这正是它。我不敢相信这是多么简单,而且解释得很好。非常感谢。
    • 太棒了。很高兴我能帮上忙!如果它解决了您的问题,请接受答案。
    • 哦!看来我说话有点太早了!运动感觉不错,但事实证明我仍然无法从水平移动到垂直移动。 ://
    • 您可以尝试使用if (vertical &gt; horizontal){...}else if(horizontal &gt; vertical) {...} else {...} 之类的构造。通常,您不想将浮点值与完全零进行比较。
    • 抱歉,您当然需要围绕这些值执行 Math.Abs​​。类似于: if (Math.Abs​​(vertical) > Math.Abs​​(horizo​​ntal)){...}else if(Math.Abs​​(horizo​​ntal) > Math.Abs​​(vertical)) {...} else {.. .}.
    猜你喜欢
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多