我如何以最好的方式告诉主导 x 或 y 方向?

How do I tell dominate x or y direction the best way possible?

好的,我正在 Unity 中制作视频游戏,目前正在研究 2D 四向运动状态机。我的空闲动画不知道要走什么路。我已经阅读了这段代码 x 100,我几乎肯定有更好的方法...

X 和 Y 是浮点数,因此我需要将它们与 Epsilon 进行比较。我已经完成了一个 switch 语句,现在是这个 if 语句。最终目标是一次计算 switch 语句,因为我基本上在这里做 4 个 if 语句

private void SetCharacterDirection(float x, float y)
{
    if (Mathf.Abs(x) - Mathf.Abs(y) > Mathf.Epsilon)
    {
        _characterDirection = Mathf.Abs(x) > Mathf.Epsilon ? CharacterDirections.Right : CharacterDirections.Left;
    }
    else
    {
        _characterDirection = Mathf.Abs(y) > Mathf.Epsilon ? CharacterDirections.Up : CharacterDirections.Down;
    }
}

有一些神奇的组合使用绝对值、余弦、math.floor()、math.ceiling,甚至是向量2;但我一直在看这个很长时间,看不到它。请帮忙。

听起来你只是想要基于具有最大幅度的输入分量的上、下、左或右。

试试这个(我还没有编译这个)

private void SetCharacterDirection(float x, float y)
{
    var absX = Mathf.Abs(x);
    var absY = Mathf.Abs(y);

    if (absX > absY)
    {
      _characterDirection = Mathf.Sign(x) > 0 ? CharacterDirections.Right : CharacterDirections.Left;
    }
    else if (absX < absY)
    {
      _characterDirection = Mathf.Sign(y) > 0 ? CharacterDirections.Up : CharacterDirections.Down;
    }
    // implicitly ignore case where X == Y
}