【问题标题】:How to limit angles realistically?如何现实地限制角度?
【发布时间】:2019-05-03 18:24:07
【问题描述】:

我的游戏角色是一只猫;它可以走路和跳跃。问题是地面有坡度和坑洞,所以猫有时会仰面翻身,像这样:

是否可以实际限制旋转角度? IE。 -45 到 45 度,因此它与物理引擎兼容。

我找到了将角度直接设置为transform.rotation = desired Rotation 的解决方案,但它会产生具有物理性质的伪影,并且当对象开始旋转并突然停止时,它看起来并不真实。

UPD:我尝试了从答案中提供的解决方案:

private void FixRotation()
{
    var angles = transform.eulerAngles;
    Debug.Log("Limiting angles: " + minAngle + "; " + maxAngle);
    Debug.Log("Rotation Z: " + angles.z);
    angles.z = Mathf.Clamp(angles.z, minAngle, maxAngle);
    transform.eulerAngles = angles;
    Debug.Log("Fixed rotation Z: " + transform.eulerAngles.z);
}

它不能正常工作。当猫要旋转更多限制时,它会在我需要时返回但是,精灵开始非常快速地摇晃。我在做什么?

【问题讨论】:

  • 查看 Mathf.clamp 并尝试不同的角度范围 docs.unity3d.com/ScriptReference/Mathf.Clamp.html
  • 它对我不起作用。你能看看我的功能吗,请问我已经更新了我的问题吗?
  • 当我切换到欧拉角时,它开始限制角度,但看起来很疯狂。猫的精灵摇晃着试图将旋转恢复到极限。

标签: unity3d unity3d-2dtools


【解决方案1】:

我使用this answer 的解决方案解决了晃动问题。

private float ClampAngle(float angle, float min, float max)
{
    if (angle < 90 || angle > 270)
    {   
        // if angle in the critic region...
        if (angle > 180)
        {
            angle -= 360;  // convert all angles to -180..+180
        }

        if (max > 180)
        {
            max -= 360;
        }

        if (min > 180)
        {
            min -= 360;
        }
    }

    angle = Mathf.Clamp(angle, min, max);

    if (angle < 0)
    {
        angle += 360;  // if angle negative, convert to 0..360
    }

    return angle;
}

private void FixRotation()
{
    var angles = transform.eulerAngles;
    angles.z = ClampAngle(angles.z, minAngle, maxAngle);
    transform.eulerAngles = angles;
}

我们需要防止游戏对象在其角度处于临界区域(或多或少 180)时完全旋转的解决方案

【讨论】:

    猜你喜欢
    • 2016-04-02
    • 2023-03-10
    • 1970-01-01
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多