unity中向量沿任意轴旋转任意角度

在 Unity 中,可以使用 Quaternion.AngleAxis 来沿任意轴旋转一个向量任意角度。具体步骤如下:

方法步骤:

  1. 指定旋转角度和旋转轴: 使用 Quaternion.AngleAxis(float angle, Vector3 axis),其中 angle 是旋转的角度(单位为度数),axis 是旋转的轴(使用 Vector3 表示)。

  2. 应用旋转: 将旋转应用到你想旋转的向量上,使用 Quaternion * Vector3,这是 Quaternion 在 Unity 中旋转向量的方式。

代码示例: 

// 定义你要旋转的向量
Vector3 originalVector = new Vector3(1, 0, 0); // 比如是沿x轴的向量

// 定义旋转的角度和轴
float angle = 90f;  // 旋转角度90度
Vector3 axis = new Vector3(0, 1, 0); // 沿y轴旋转

// 生成旋转的四元数
Quaternion rotation = Quaternion.AngleAxis(angle, axis);

// 应用旋转到向量上
Vector3 rotatedVector = rotation * originalVector;

// 输出结果
Debug.Log("旋转后的向量: " + rotatedVector);

在这个例子中,原始向量 (1, 0, 0) 沿着 Y 轴旋转 90 度,旋转后的向量结果为 (0, 0, -1)

注意:

在 Unity 中,rotation * originalVectororiginalVector * rotation 是不可以互换的。

使用 Quaternion 来旋转一个向量时,四元数必须放在左侧,即 rotation * originalVector。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值