Unity3D学习笔记(八):四元素和书籍推荐
书籍推荐:
3D数学基础:图形与游戏开发——游戏软件开发专家系列(美)邓恩
Unity Shader入门精要 冯乐乐(92年)
数据结构(Python语言描述)
数据结构、算法与应用(C++语言描述)
算法导论 第3版
黑客与画家
程序员生存之道
流体力学:八叉数空间划分
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateVector : MonoBehaviour {
// Use this for initialization
void Start () {
Vector3 dir = new Vector3(,,);
Debug.DrawLine(Vector3.zero,dir,Color.red,);
//单位四元数表示无旋转
Debug.Log(Quaternion.identity);
//四元数的逆
Quaternion q = Quaternion.AngleAxis(, Vector3.up);
Quaternion tempQ = Quaternion.Inverse(q);
Debug.Log(q);
Debug.Log(tempQ);
//旋转四元数
Quaternion rotateQ = Quaternion.AngleAxis(, Vector3.up);
Vector3 newVector = rotateQ * dir;
Debug.DrawLine(Vector3.zero, newVector, Color.green, );
//自己写一个RotationAround
//RotateAround(Vector3 point, Vector3 axis, float angle);
}
// Update is called once per frame
void Update () { }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateAroundScript : MonoBehaviour {
public Transform sphere;
public float rotateSpeed = ;
// Use this for initialization
void Start () { } // Update is called once per frame
void 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),
/Quaternion.Angle(transform.rotation, Quaternion.LookRotation(target.position - transform.position)));
//4)、把一个欧拉角转换为四元数
transform.rotation = Quaternion.Euler(, , );
//5)、使用鼠标的水平轴值控制当前物体旋转(使用四元数叠加)
float mouseX = Input.GetAxis("Mouse X");
//自身的 绕Y轴转 XX 度
transform.rotation *= Quaternion.Euler(, mouseX, );
//世界的 绕Y轴转 XX 度
transform.rotation = Quaternion.Euler(, mouseX, ) * transform.rotation;
void Start () {
transform.rotation = Quaternion.AngleAxis(, Vector3.right);
transform.eulerAngles = new Vector3(, , );
transform.LookAt(target);
transform.rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Euler(, , );
} void Update () {
transform.rotation = Quaternion.Slerp(
transform.rotation,
Quaternion.LookRotation(target.position - transform.position),
/ Quaternion.Angle(transform.rotation, Quaternion.LookRotation(target.position - transform.position)));
float mouseX = Input.GetAxis("Mouse X");
// 自身的 绕Y轴转 XX 度
transform.rotation *= Quaternion.Euler(, mouseX, );
// 世界的 绕Y轴转 XX 度
transform.rotation = Quaternion.Euler(, mouseX, ) * transform.rotation;
}
private void OnDrawGizmos()
{
Gizmos.color = new Color(, , , 0.2f);
Gizmos.DrawSphere(transform.position, );
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawCube(transform.position, cubeSize);
} }
Unity3D学习笔记(八):四元素和书籍推荐的更多相关文章
- Unity3D学习笔记(四)Unity的网络基础(C#)
一 网络下载可以使用WWW类下载资源用法:以下载图片为例WWW date = new WWW("<url>");yield return date;texture = ...
- [转]Unity3D学习笔记(四)天空、光晕和迷雾
原文地址:http://bbs.9ria.com/thread-186942-1-1.html 作者:江湖风云 六年前第一次接触<魔兽世界>的时候,被其绚丽的画面所折服,一个叫做贫瘠之地的 ...
- Unity3D学习笔记(四):物理系统碰撞和预制体
Rigidbody(刚体组件):加了此组件游戏物体就变成刚体了 ----Mass(质量,单位kg):重力G = 质量m * 重力加速度g(g=9.81 m/s^2) --------冲量守恒定理 动量 ...
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例 新浪爱彩双色球开奖数据URL:http://zst.aicai.com/ssq/openInfo/ 最终输出结果格 ...
- X-Cart 学习笔记(四)常见操作
目录 X-Cart 学习笔记(一)了解和安装X-Cart X-Cart 学习笔记(二)X-Cart框架1 X-Cart 学习笔记(三)X-Cart框架2 X-Cart 学习笔记(四)常见操作 五.常见 ...
- C++Primer第5版学习笔记(四)
C++Primer第5版学习笔记(四) 第六章的重难点内容 你可以点击这里回顾第四/五章的内容 第六章是和函数有关的知识,函数就是命名了的代码块,可以处理不同的情况,本章内 ...
- Go语言学习笔记八: 数组
Go语言学习笔记八: 数组 数组地球人都知道.所以只说说Go语言的特殊(奇葩)写法. 我一直在想一个人参与了两种语言的设计,但是最后两种语言的语法差异这么大.这是自己否定自己么,为什么不与之前统一一下 ...
- 【转】 Pro Android学习笔记(四十):Fragment(5):适应不同屏幕或排版
目录(?)[-] 设置横排和竖排的不同排版风格 改写代码 对于fragment,经常涉及不同屏幕尺寸和不同的排版风格.我们在基础小例子上做一下改动,在横排的时候,仍是现实左右两个fragment,在竖 ...
- 【opencv学习笔记八】创建TrackBar轨迹条
createTrackbar这个函数我们以后会经常用到,它创建一个可以调整数值的轨迹条,并将轨迹条附加到指定的窗口上,使用起来很方便.首先大家要记住,它往往会和一个回调函数配合起来使用.先看下他的函数 ...
随机推荐
- 论存储IOPS和Throughput吞吐量之间的关系
论存储IOPS和Throughput吞吐量之间的关系 http://www.csdn.net/article/2015-01-14/2823552 IOPS和Throughput吞吐量两个参数是衡量存 ...
- 【深入理解javascript】this的用法
引用:this的用法 在函数中this到底取何值,是在函数真正被调用执行的时候确定的,函数定义的时候确定不了 情况1:构造函数 函数作为构造函数用,那么其中的this就代表它即将new出来的对象.另外 ...
- vue之vue-cookies
vue之vue-cookies npm链接:https://www.npmjs.com/package/vue-cookies 安装: npm install vue-cookies --save 使 ...
- 浙大 PAT 乙级 1001-1075 目录索引
1001. 害死人不偿命的(3n+1)猜想 1002. 写出这个数 1003. 我要通过! 1004. 成绩排名 1005. 继续(3n+1)猜想 1006. 换个格式输出整数 1007. 素数对猜想 ...
- UBUNTU16.04 使用APT-GET如何设置代理
sudo apt-get install software-name -o Acquire::http::proxy="http://用户名:密码@代理服务器IP:代理服务器端口&quo ...
- http://ttaa.210997.com/恶意修改主页
嗯,,,,之前似乎写过关于篡改主页的文章. 但今天下了个游戏,然后不小心又出现了这个问题. 我先用原始的方式检测了一下(比如检索注册表之类的),但这个不奏效. 省略一些查看问题的方式. 最终得出:新的 ...
- html5<embed>的完整属性
问题起因:网页中插入Flash,看了代码,然后呢,小小的学习下 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000& ...
- Trove系列(一)—入门篇
概述DBaaS是目前云计算服务的重要部分,数据库作为一种特殊的应用程序,在应用中普遍存在.而其独特性不仅在于普遍性,而且其性能对应用的表现是至关重要的.数据库的通用性和重要性使得维护一个健壮的数据库实 ...
- EditPlus 4.3.2543 中文版已经发布(2月3日更新,Emmet 功能回归)
新的 EditPlus 版本修复了 Emmet 组件的安全问题. 现在 Emmet 编辑功能又回来啦. 下载连接在页面左上角!
- python+Django框架运用(三)
Django模型 模式指的是根据数据库中数据表的结构来创建出来的class,每一张表到Python中就是一个 class,表中的每一个列,到Python中就是class的一个属性. 在模型中可以完成对 ...