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

Unity C#中Quaternion无法用减运算符合并旋转值求助

Unity四元数旋转运算错误解决方案
  • 错误根源:Quaternion类型未定义减法运算符,无法直接用-操作两个四元数。Unity中四元数的旋转合并/相对旋转计算,必须通过逆运算+乘法实现,不能用加减。

修正后的代码

public GameObject Cube;
public GameObject Img;
public GameObject child;

// 计算Cube相对于Img的局部旋转:用Img旋转的逆抵消自身旋转,再乘Cube的旋转
Quaternion localRotationValue = Quaternion.Inverse(Img.transform.localRotation) * Cube.transform.localRotation;
Instantiate(child, Vector.zero, localRotationValue);

关键说明

  • 四元数乘法顺序决定旋转逻辑:A * B代表先执行B的旋转,再执行A的旋转。这里用Inverse(ImgRot) * CubeRot,目的是得到Cube相对于Img的旋转差值。
  • 若需求是叠加两个旋转(比如先转Img的角度再转Cube的角度),直接用Cube.transform.localRotation * Img.transform.localRotation即可,注意顺序会影响最终旋转结果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 13:17:04