Unity3D学习笔记(八):四元素和书籍推荐
书籍推荐:
3D数学基础:图形与游戏开发——游戏软件开发专家系列(美)邓恩 Unity Shader入门精要 冯乐乐(92年) 数据结构(Python语言描述) 数据结构、算法与应用(C++语言描述) 算法导论 第3版 黑客与画家 程序员生存之道 流体力学:八叉数空间划分
using System.Collections; using System.Collections.Generic; using UnityEngine; public class RotateVector : MonoBehaviour {// Use this for initializationvoid Start () {Vector3 dir = new Vector3(1,0,1);Debug.DrawLine(Vector3.zero,dir,Color.red,1000);//单位四元数表示无旋转 Debug.Log(Quaternion.identity);//四元数的逆Quaternion q = Quaternion.AngleAxis(90, Vector3.up);Quaternion tempQ = Quaternion.Inverse(q);Debug.Log(q);Debug.Log(tempQ);//旋转四元数Quaternion rotateQ = Quaternion.AngleAxis(45, Vector3.up);Vector3 newVector = rotateQ * dir;Debug.DrawLine(Vector3.zero, newVector, Color.green, 1000);//自己写一个RotationAround//RotateAround(Vector3 point, Vector3 axis, float angle); }// Update is called once per framevoid Update () {} }
![](/assets/blank.gif)
using System.Collections; using System.Collections.Generic; using UnityEngine; public class RotateAroundScript : MonoBehaviour {public Transform sphere;public float rotateSpeed = 1;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {//调用RotateAround RotateAround(sphere.position, Vector3.up, rotateSpeed);}public void myRotateAround(Vector3 point, Vector3 axis, float angle){Vector3 A = transform.position - point;//Vector3 n = Vector3.Project();//Vector3.Dot(axis, A) = | n | * axis.magnitude;//| n | = Vector3.Dot(axis, A) / axis.magnitude;//Vector3 n = axis.normalized * | n |;Vector3 n = axis.normalized * Vector3.Dot(axis, A) / axis.magnitude;Vector3 a = A - n;Quaternion q = Quaternion.AngleAxis(angle, axis);Vector3 b = q * a;} }
复习
using System.Collections; using System.Collections.Generic; using UnityEngine; public class w06d4 : MonoBehaviour {public Transform target;public Vector3 cubeSize;//欧拉角是如何表示旋转的//使用x y z来表示旋转 x表示绕x轴旋转多少度 y表示绕y轴旋转多少度 z表示绕z轴旋转多少度//四元数是如何表示旋转的//使用x y z w来表示旋转 通过轴角对来表示旋转 n轴转45度 x = n.x * sin<theta/2> , y = n.y * sin<theta/2>, z = n.z * sin<theta/2>, w = cos<theta/2>//欧拉角和四元数的优缺点//欧拉角// 优点: 直观,容易理解// 缺点: 有万向节锁// 只能绕固定轴旋转// 不能实现旋转的平滑差值 //四元数// 优点: 可以实现任何角度的旋转// 可以实现旋转叠加// 没有万向节锁 、// 可以绕任意轴旋转// 可以实现旋转的平滑差值 // 缺点: 不直观,不容易理解//API//使当前游戏物体的旋转变为 绕x轴旋转90//1)、使用欧拉角的形式赋值 //transform.eulerAngles = new Vector3(90, 0, 0);// 使用四元数的形式赋值 //transform.rotation = Quaternion.AngleAxis(90, Vector3.right);//2)、使当前游戏物体直接看向目标 transform.LookAt(target);transform.rotation = Quaternion.LookRotation(target.position - transform.position);//3)、使当前游戏物体缓缓看向目标transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(target.position - transform.position),1/Quaternion.Angle(transform.rotation, Quaternion.LookRotation(target.position - transform.position)));//4)、把一个欧拉角转换为四元数transform.rotation = Quaternion.Euler(0, 45, 0);//5)、使用鼠标的水平轴值控制当前物体旋转(使用四元数叠加)float mouseX = Input.GetAxis("Mouse X");//自身的 绕Y轴转 XX 度transform.rotation *= Quaternion.Euler(0, mouseX, 0);//世界的 绕Y轴转 XX 度transform.rotation = Quaternion.Euler(0, mouseX, 0) * transform.rotation;void Start () {transform.rotation = Quaternion.AngleAxis(90, Vector3.right);transform.eulerAngles = new Vector3(90, 0, 0);transform.LookAt(target);transform.rotation = Quaternion.LookRotation(target.position - transform.position);transform.rotation = Quaternion.Euler(0, 45, 0);}void Update () {transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(target.position - transform.position),1 / Quaternion.Angle(transform.rotation, Quaternion.LookRotation(target.position - transform.position)));float mouseX = Input.GetAxis("Mouse X");// 自身的 绕Y轴转 XX 度transform.rotation *= Quaternion.Euler(0, mouseX, 0);// 世界的 绕Y轴转 XX 度transform.rotation = Quaternion.Euler(0, mouseX, 0) * transform.rotation;}private void OnDrawGizmos(){Gizmos.color = new Color(0, 1, 0, 0.2f);Gizmos.DrawSphere(transform.position, 10);}private void OnDrawGizmosSelected(){Gizmos.color = Color.red;Gizmos.DrawCube(transform.position, cubeSize);}}
转载于:https://www.cnblogs.com/cnwuchao/p/10362882.html
Unity3D学习笔记(八):四元素和书籍推荐相关推荐
- Unity3D学习笔记(四)Unity的网络基础(C#)
一 网络下载 可以使用WWW类下载资源 用法:以下载图片为例 WWW date = new WWW("<url>"); yield return date; textu ...
- Unity3D学习笔记(四)天空、光晕和迷雾
六年前第一次接触<魔兽世界>的时候,被其绚丽的画面所折服,一个叫做贫瘠之地的地方,深深印在我的脑海里.当时在艾泽拉斯大陆还不能使用飞行坐骑,试想一下孤身一人在银色的月光下赶路,可能还有你的 ...
- Unity3D学习笔记(四)牧师和魔鬼游戏改进
在上次做的牧师和魔鬼的游戏中,场记基本负责了所有的工作,加载资源,移动游戏对象,很明显这样的游戏结构很难维护,很难拓展,很不面向对象,所以这次的工作就是利用工厂模式来生产动作.弥补了上次没有上船动作的 ...
- unity3d 学习笔记四 skybox(天空盒) light(光源) halo(光晕)
Unity3D学习笔记(四)天空.光晕和迷雾 六年前第一次接触<魔兽世界>的时候,被其绚丽的画面所折服,一个叫做贫瘠之地的地方,深深印在我的脑海里.当时在艾泽拉斯大陆还不能使用飞行坐骑,试 ...
- OpenCV学习笔记(四十六)——FAST特征点检测features2D OpenCV学习笔记(四十七)——VideoWriter生成视频流highgui OpenCV学习笔记(四十八)——PCA算
OpenCV学习笔记(四十六)--FAST特征点检测features2D 特征点检测和匹配是计算机视觉中一个很有用的技术.在物体检测,视觉跟踪,三维常年关键等领域都有很广泛的应用.这一次先介绍特征点检 ...
- 黑马程序员_java自学学习笔记(八)----网络编程
黑马程序员_java自学学习笔记(八)----网络编程 android培训. java培训.期待与您交流! 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很长一段时间无 ...
- OpenGL学习笔记(八):进一步理解VAO、VBO和SHADER,并使用VAO、VBO和SHADER绘制一个三角形
原博主博客地址:http://blog.csdn.net/qq21497936 本文章博客地址:http://blog.csdn.net/qq21497936/article/details/7888 ...
- OpenCV学习笔记(四十一)——再看基础数据结构core OpenCV学习笔记(四十二)——Mat数据操作之普通青年、文艺青年、暴力青年 OpenCV学习笔记(四十三)——存取像素值操作汇总co
OpenCV学习笔记(四十一)--再看基础数据结构core 记得我在OpenCV学习笔记(四)--新版本的数据结构core里面讲过新版本的数据结构了,可是我再看这部分的时候,我发现我当时实在是看得太马 ...
- Windows x64内核学习笔记(四)—— 9-9-9-9-12分页
Windows x64内核学习笔记(四)-- 9-9-9-9-12分页 前言 9-9-9-9-12分页 实验一:线性地址转物理地址 页表基址 定位基址 PTE to PXE 实验二:通过页表基址定位各 ...
最新文章
- USEARCH —— 最简单易学的扩增子分析流程(中国总代理)
- python简单代码画曲线图教程-Python绘制折线图和散点图的详细方法介绍(代码示例)...
- 刷脏页策略linux,linux – 生成大量脏页阻止同步写入
- Bootstrap学习笔记--常用标签和类模板
- oracle数据库无法查询,关于oracle中无法查询中文条件的解决办法
- 微信小程序自定义弹窗组件
- 接口自动化测试框架实例教程
- java实现电子面单pdf生成_快递鸟电子面单接口打印实现代码
- 自学iOS开发的一些经验
- Hibernate的Disjunction和Conjunction
- 由于navigation引起viewwillappear不被调用
- 各种群体寻优算法的比较
- 规格中的OR\DR\DS\RR\IR\PB\SF\SR\AR\CR
- 50个明星区块链项目跌破发行价,超10家项目几近归零
- Differences between Theorem, Lemma and Corollary, etc.
- ARGB颜色快速取大对比度色(保留原本透明度)
- Unity 3D 脚本参考
- 1、	输入长方形的长和宽,编程求该长方形的周长和面积
- 理解用户模式和内核模式(译)
- 通检检测技术有限公司-欧盟国家玩具类检测认证
热门文章
- 人工智能——人工智能研究的各种学派及其理论
- 流量卡之家:运营商7月份运营数据 中国电信移动用户首超中国联通
- surfacebook跑matlab,微软Surface book开坑:用TLC闪存,写入速度太渣
- 如何实现 楼中楼评论(盖楼)spring boot + mybatis 附完整代码
- 一文读懂 Uniswap 做市商协议 Multiple Protocol
- wifi 荣耀手机usb_【已解决】电脑端Win7通过WIFI无线网络共享操作安卓手机华为荣耀6的文件...
- 爱用建站智能推送一键完成全网分发,你也可以篇篇10w+
- android 获取nfc,Android:在服务类中读取NFC标签
- Directx跟opengl有什么不同
- 如何制作抖音短视频搬运RPA机器人?