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精确小数计算技术体系,感兴趣的小伙伴可以了解一下... 目录引言:小数精度问题的核心挑战一、浮点数精度问题分析1.1 浮点数精度陷阱1.2 浮点数误差来源二、基础解决

Python文本相似度计算的方法大全

《Python文本相似度计算的方法大全》文本相似度是指两个文本在内容、结构或语义上的相近程度,通常用0到1之间的数值表示,0表示完全不同,1表示完全相同,本文将深入解析多种文本相似度计算方法,帮助您选... 目录前言什么是文本相似度?1. Levenshtein 距离(编辑距离)核心公式实现示例2. Jac

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

C#和Unity中的中介者模式使用方式

《C#和Unity中的中介者模式使用方式》中介者模式通过中介者封装对象交互,降低耦合度,集中控制逻辑,适用于复杂系统组件交互场景,C#中可用事件、委托或MediatR实现,提升可维护性与灵活性... 目录C#中的中介者模式详解一、中介者模式的基本概念1. 定义2. 组成要素3. 模式结构二、中介者模式的特点

Python中经纬度距离计算的实现方式

《Python中经纬度距离计算的实现方式》文章介绍Python中计算经纬度距离的方法及中国加密坐标系转换工具,主要方法包括geopy(Vincenty/Karney)、Haversine、pyproj... 目录一、基本方法1. 使用geopy库(推荐)2. 手动实现 Haversine 公式3. 使用py

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Java计算经纬度距离的示例代码

《Java计算经纬度距离的示例代码》在Java中计算两个经纬度之间的距离,可以使用多种方法(代码示例均返回米为单位),文中整理了常用的5种方法,感兴趣的小伙伴可以了解一下... 目录1. Haversine公式(中等精度,推荐通用场景)2. 球面余弦定理(简单但精度较低)3. Vincenty公式(高精度,

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

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

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

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

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