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

如何绕另一向量旋转Vector3以得到结果向量?

实现3D环境下的Vector3绕枢轴点旋转方法

直接上可复用的方法实现,基于Unity的Quaternion处理旋转(匹配你提到的用Quaternion.eulerAngles的需求):

Vector3 RotateAroundPivot(Vector3 target, Vector3 pivot, Quaternion rotation)
{
    // 1. 将目标点转换为相对于枢轴的局部坐标
    Vector3 localPoint = target - pivot;
    // 2. 用四元数旋转局部坐标向量
    Vector3 rotatedLocalPoint = rotation * localPoint;
    // 3. 转换回世界坐标(加上枢轴点)
    return rotatedLocalPoint + pivot;
}

使用示例

比如你要把Vector3(1, 0, 0)绕原点Vector3(0,0,0)绕Y轴旋转90度,调用方式如下:

// 创建绕Y轴旋转90度的四元数
Quaternion rotation = Quaternion.Euler(0, 90, 0);
Vector3 answer = RotateAroundPivot(new Vector3(1, 0, 0), Vector3.zero, rotation);
// 此时answer的值为(0, 0, 1)

关键逻辑说明

  • 先把目标点减去枢轴点,得到相对于枢轴的偏移向量,这样旋转可以基于枢轴的局部空间进行,避免直接旋转世界坐标带来的偏移问题。
  • 四元数是Unity中处理3D旋转的标准方式,Quaternion.Euler(x,y,z)可以直接通过欧拉角创建旋转,对应你说的Quaternion.eulerAngles使用场景。
  • 旋转完成后把偏移向量加回枢轴点,得到最终的世界坐标位置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 14:27:13