Unity之四元数计算

2024-01-20 19:52
文章标签 计算 unity 四元

本文主要是介绍Unity之四元数计算,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

四元数相乘

#region 四元数相乘
Quaternion q = Quaternion.AngleAxis(20, Vector3.up);
this.transform.rotation *= q;
#endregion

四元数乘向量

Vector3 v = Vector3.forward;
print(v);
//四元数乘向量的顺序不能改变,也就是说不能用向量去乘四元数,只能是四元数乘向量
v = Quaternion.AngleAxis(45,Vector3.up) * v;
print(v);
v = Quaternion.AngleAxis(45, Vector3.up) * v;
print(v);

例一

模拟飞机发射不同类型子弹的方法,单发,双发,扇形,环形

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public enum E_FireType
{One,Two,Three,Round
}
public class AirPlane : MonoBehaviour
{private E_FireType nowType;public GameObject bullet;public int roundNum = 4;// Start is called before the first frame updatevoid Start(){nowType = E_FireType.One;}// Update is called once per framevoid Update(){//切换开火方式if (Input.GetKeyDown(KeyCode.Alpha1)){nowType = E_FireType.One;}else if (Input.GetKeyDown(KeyCode.Alpha2)){nowType = E_FireType.Two;}else if (Input.GetKeyDown(KeyCode.Alpha3)){nowType = E_FireType.Three;}else if (Input.GetKeyDown(KeyCode.Alpha4)){nowType = E_FireType.Round;}if(Input.GetKeyDown(KeyCode.Space)){Fire();}}private void Fire(){switch (nowType){case E_FireType.One:Instantiate(bullet,transform.position,transform.rotation);break;case E_FireType.Two://让发射位置分别向两边的方向偏移一点Instantiate(bullet, transform.position - transform.right * 1, transform.rotation);Instantiate(bullet, transform.position + transform.right * 1, transform.rotation);break;case E_FireType.Three:Instantiate(bullet, transform.position, transform.rotation);Instantiate(bullet, transform.position - transform.right * 1, transform.rotation * Quaternion.AngleAxis(-20, Vector3.up));Instantiate(bullet, transform.position + transform.right * 1, transform.rotation * Quaternion.AngleAxis(20, Vector3.up));break;case E_FireType.Round:float angle = 360/roundNum;for(int i = 0; i < roundNum; i++){Instantiate(bullet, this.transform.position, this.transform.rotation * Quaternion.AngleAxis(i * angle, Vector3.up));}break;}}
}

练习二

用3D数学知识实现摄像机跟随效果

1.摄像机在人物斜后方,通过角度控制倾斜率

2.通过鼠标滚轮控制摄像机距离人物的距离(有最大最小限制)

3.摄像机看向任务头顶上方的一个位置(可调节)

4.Vector3.Lerp实现相机跟随任务

5.Quaternion.Slerp实现摄像机看向人物

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CameraMove2 : MonoBehaviour
{//目标对象public Transform target;//相对头顶偏移多少public float headOffsetH = 1;//倾斜角度public float angle = 45;//默认的摄像机里观测点的距离public float dis = 5;//距离必须是3和10之间 public float minDis = 3;public float maxDis = 10;//当前摄像机应该在的位置private Vector3 nowPos;private Vector3 nowDir;private float time;private Vector3 startPos;private Vector3 endPos;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){//鼠标中键滚动控制摄像机远近的逻辑dis += Input.GetAxis("Mouse ScrollWheel");dis = Mathf.Clamp(dis, minDis, maxDis);//向头顶偏移位置nowPos = target.position + target.up * headOffsetH;//往后方偏移位置 nowDir = Quaternion.AngleAxis(angle, target.right) * -target.forward;nowPos = nowPos + nowDir * dis;//直接把算出来的位置赋值if(endPos != nowPos) { startPos = this.transform.position;endPos = nowPos;time = 0;}time += Time.deltaTime;//this.transform.position = nowPos;//先快后慢//this.transform.position = Vector3.Lerp(this.transform.position,nowPos,Time.deltaTime*dis);//匀速this.transform.position = Vector3.Lerp(startPos, endPos, time);Quaternion.LookRotation(nowDir);this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(-nowDir),Time.deltaTime);}
}

这篇关于Unity之四元数计算的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/627148

相关文章

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

如何用Java结合经纬度位置计算目标点的日出日落时间详解

《如何用Java结合经纬度位置计算目标点的日出日落时间详解》这篇文章主详细讲解了如何基于目标点的经纬度计算日出日落时间,提供了在线API和Java库两种计算方法,并通过实际案例展示了其应用,需要的朋友... 目录前言一、应用示例1、天安门升旗时间2、湖南省日出日落信息二、Java日出日落计算1、在线API2

poj 1113 凸包+简单几何计算

题意: 给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。 解析: 韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。 用convexhull算法把凸包求出来,然后加加减减就A了。 计算见下图: 好久没玩画图了啊好开心。 代码: #include <iostream>#include <cstdio>#inclu

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc

uva 11178 计算集合模板题

题意: 求三角形行三个角三等分点射线交出的内三角形坐标。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <

XTU 1237 计算几何

题面: Magic Triangle Problem Description: Huangriq is a respectful acmer in ACM team of XTU because he brought the best place in regional contest in history of XTU. Huangriq works in a big compa

音视频入门基础:WAV专题(10)——FFmpeg源码中计算WAV音频文件每个packet的pts、dts的实现

一、引言 从文章《音视频入门基础:WAV专题(6)——通过FFprobe显示WAV音频文件每个数据包的信息》中我们可以知道,通过FFprobe命令可以打印WAV音频文件每个packet(也称为数据包或多媒体包)的信息,这些信息包含该packet的pts、dts: 打印出来的“pts”实际是AVPacket结构体中的成员变量pts,是以AVStream->time_base为单位的显

计算数组的斜率,偏移,R2

模拟Excel中的R2的计算。         public bool fnCheckRear_R2(List<double[]> lRear, int iMinRear, int iMaxRear, ref double dR2)         {             bool bResult = true;             int n = 0;             dou

GPU 计算 CMPS224 2021 学习笔记 02

并行类型 (1)任务并行 (2)数据并行 CPU & GPU CPU和GPU拥有相互独立的内存空间,需要在两者之间相互传输数据。 (1)分配GPU内存 (2)将CPU上的数据复制到GPU上 (3)在GPU上对数据进行计算操作 (4)将计算结果从GPU复制到CPU上 (5)释放GPU内存 CUDA内存管理API (1)分配内存 cudaErro