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

Unity旋转范围转换(-180~180转0~360)及旋转代码问题求助

Unity旋转值范围问题及旋转逻辑失效求助
  • 脚本修改Unity旋转值时,范围限定在*-180到180*;但在Inspector面板中修改旋转值却可以无限增大,暂时不清楚其中原因。
  • 目前遇到的问题:需要让物体从当前旋转值逆时针旋转回到0度。当旋转值不超过180度时一切正常,但超过该值后旋转代码失效,无法让物体旋转超过半圈。个人认为使用0到360的标准旋转范围会更便于编码。

补充:抱歉回复较晚,以下是相关代码:

[ContextMenu("Rotate Chamber")]
public void RotateChamber()
{
    StartCoroutine(RotateOverTime(60, 1f, true));
}

[ContextMenu("Reload Chamber")]
public void Reload()
{
    float currentAngle = -chamber.localEulerAngles.z;
    StartCoroutine(RotateOverTime(currentAngle, 5f, false));
}

private IEnumerator RotateOverTime(float angle, float duration, bool clockWiseRotation)
{
    Quaternion startRotation = chamber.localRotation;
    Quaternion targetRotation = chamber.localRotation * Quaternion.Euler(0, 0, clockWiseRotation ? -angle : angle);

    float elapsedTime = 0f;
    while (elapsedTime < duration)
    {
        chamber.localRotation = Quaternion.Lerp(startRotation, targetRotation, elapsedTime / duration);
        elapsedTime += Time.deltaTime;
        yield return null;
    }

    chamber.localRotation = targetRotation;
}

不确定这段基础代码能提供多少帮助,但仍感谢协助。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 00:12:06