【第018问 Unity中对Quaternion.AngleAxis的理解?】

本文介绍了Unity中Quaternion.AngleAxis的使用,详细讲解了如何围绕轴旋转物体以及世界坐标轴与本地坐标轴旋转的区别。通过实例展示了Quaternion相乘的原理,并提供了源代码实现,帮助读者理解旋转的叠加效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、背景

在游戏研发过程中,有时候会对一个物体经过多次围绕不同的轴进行不同角度的旋转,从而计算得到一个方向,以此来检测在该对应的方向上是否有对应的物体或别的对象,因此本节对Quaternion.AngleAxis进行简单的记录;

二、Quaternion.AngleAxis

对该API进行理解:

  • Quaternion:四元数,这个在Unity中基本就是用于计算方位、旋转的;
  • AngleAxis:这个就表示围绕一个轴,进行指定的角度旋转

整体就是:指定一个轴,围绕该轴旋转指定的角度,得到一个新的旋转结果

三、实践

1、动画演示请添加图片描述

在上图中可以看到有两个Cube,都在以相同的角度,围绕相同的轴在旋转;这里只是简单的用了该API的结果:

 targetA.rotation = Quaternion.AngleAxis(angel, direction);
 targetB.rotation = Quaternion.AngleAxis(angel, direction);

Angel:代表旋转多少度,比如上图中的滑动条控制的数值0-180;当然0是不会有任何变化的;

direction:代表的是轴,也就是围绕哪一个轴进行旋转;上面的动画中只是采用基本的
Vector3.up、Vector3.right,Vector3.forward进行基本的演示。但在实际的操作中的方向大多数不是这几个基本的的方向,有更多其它的方向。

2、世界坐标轴和本地坐标轴旋转差异

动画演示:
请添加图片描述
首先将CubeA和CubeB都调整一个相同的旋转角度;然后在分别在有右侧的面板中调节参数,对它们进行旋转;可以发现旋转的实际效果是不一样的【可以根据xyz轴的方向来发明细发现】;这是因为旋转的时候采用的坐标轴不一样;
CubeA:采用世界坐标轴的方式进行旋转;
CubeB:采用的是CubeB本身的坐标轴进行旋转的;

targetA.rotation = Quaternion.AngleAxis(angel, direction);
targetB.rotation = Quaternion.AngleAxis(angel, rotationB * direction);

rotationB * direction:表示CubeB本身的指定轴的方向;比如Vector3.right表示的是右侧,但是它不能直接代表Cube本身的右侧,其中的原因是Cube本身是存在旋转的;

  • 如果CubeB不旋转,且在场景中没有父节点;或者所有的父节点都没有旋转;那么此刻CubeB右侧和Vector3.right是在同一个方向;
  • 如果CubeB旋转了,那么就需要用CubeB当前的旋转乘以Vector3.right,其结果才真正表示Cube的右侧;
3、 Quaternion相乘

如果两个旋转的方向结果相乘,那么结果会是什么:看演示
请添加图片描述
从上面的动画可以看出:

  • 对CubeA进行围绕Y轴旋转指定角度A,然后CubeB进行调节下面角度B1、B2
  • 角度A =B1+B2时,两个旋转的结果是一样的;也就是具有相同的旋转行为;
 targetA.rotation = Quaternion.AngleAxis(angel, rotationA * direction);


 targetB.rotation = Quaternion.AngleAxis(angelB1, rotationB * direction) *
                           Quaternion.AngleAxis(angelB2, rotationB * direction);

两个Quaternion.AngleAxis 相乘;也就是Quaternion相乘其实就是表示两个旋转角度累加

四、源码

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class QuaternionAngleAxis : MonoBehaviour
{
    [SerializeField] private Transform targetA;
    [SerializeField] private Transform targetB;
    [SerializeField] [Range(0, 180)] private int angel;
    [SerializeField] [Range(0, 180)] private int angelB1;
    [SerializeField] [Range(0, 180)] private int angelB2;

    [SerializeField] private bool isReset;


    public enum Axis
    {
        Up,
        Forward,
        Right,
    }


    public enum TestModel
    {
        /// <summary>
        /// 普通旋转
        /// </summary>
        Normal,

        /// <summary>
        /// 世界坐标和本身旋转
        /// </summary>
        WLRotation,

        /// <summary>
        /// 旋转相乘
        /// </summary>
        Mult,
    }

    private Quaternion rotationA;
    private Quaternion rotationB;

    public TestModel model;
    public Axis axis = Axis.Right;
    private Vector3 direction;

    private void OnDrawGizmos()
    {
        if (isReset)
        {
            rotationA = targetA.rotation;
            rotationB = targetB.rotation;
            return;
        }


        switch (axis)
        {
            case Axis.Forward:
                direction = Vector3.forward;
                break;
            case Axis.Right:
                direction = Vector3.right;
                break;
            case Axis.Up:
                direction = Vector3.up;
                break;
        }


        switch (model)
        {
            case TestModel.Normal:
                NormalAngelAxis();
                break;
            case TestModel.WLRotation:
                WLRotationAngelAxis();
                break;
            case TestModel.Mult:
                MultAngelAxis();
                break;
        }
    }


    void NormalAngelAxis()
    {
        targetA.rotation = Quaternion.AngleAxis(angel, direction);
        targetB.rotation = Quaternion.AngleAxis(angel, direction);
    }


    void WLRotationAngelAxis()
    {
        targetA.rotation = Quaternion.AngleAxis(angel, direction);
        targetB.rotation = Quaternion.AngleAxis(angel, rotationB * direction);
    }

    void MultAngelAxis()
    {
        targetA.rotation = Quaternion.AngleAxis(angel, rotationA * direction);


        targetB.rotation = Quaternion.AngleAxis(angelB1, rotationB * direction) *
                           Quaternion.AngleAxis(angelB2, rotationB * direction);
    }
}

结语:

最近好像开始长胖了–【valaki】

Unity中,要将一个物体转向其当前的正前方,即让其世界坐标旋转为0,可以使用`Quaternion.AngleAxis`结合当前物体的前方向来实现。首先,需要确定物体当前的前方向,然后计算当前前方向与目标方向(通常是世界坐标系的正前方,即Vector3.forward)之间的角度差。使用这个角度差,通过`Quaternion.AngleAxis`来创建一个新的旋转 Quaternion,然后将物体的旋转设置为这个 Quaternion。以下是具体的步骤: 1. 获取物体当前的世界坐标前方方向,通常可以使用`transform.forward`得到。 2. 计算当前前方方向与目标前方方向(Vector3.forward)的欧拉角差异。 3. 使用`Quaternion.AngleAxis`函数创建一个旋转 Quaternion,参数为步骤2计算出的角度差以及物体当前的上方向(通常是transform.up)。 4. 将物体的旋转设置为这个新创建的 Quaternion。 这里是一个示例代码,展示了如何实现这个过程: ```csharp using UnityEngine; public class RotateToForward : MonoBehaviour { public void RotateToTargetDirection() { // 目标方向是世界坐标系的正前方 Vector3 targetDirection = Vector3.forward; // 物体当前的前方方向 Vector3 currentDirection = transform.forward; // 计算旋转角度 float angle = Quaternion.Angle(targetDirection, currentDirection); // 计算旋转轴,这里是物体的上方向 Vector3 rotationAxis = Vector3.Cross(targetDirection, currentDirection).normalized; // 创建旋转的Quaternion Quaternion rotation = Quaternion.AngleAxis(angle, rotationAxis); // 应用旋转 transform.rotation = rotation; } } ``` 在使用上述代码之前,请确保物体有一个活跃的Rigidbody组件,以便物理引擎可以正确处理旋转动画。如果没有物理动画需求,则可以直接修改transform.rotation。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值