Unity3D学习笔记(九):摄像机
3D数学复习
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class w07d1 : MonoBehaviour {
public Quaternion result1;
public Quaternion result2;
public Quaternion result3;
// 什么是单位四元数?
// [0, 0, 0, 1] 表示无旋转
// [n.x * sin<theta/2>, n.y * sin<theta/2>, n.z * sin<theta/2>, cos<theta/2>] 绕y轴旋转0度 [0, 0, 0, 1] 绕y轴旋转360度 [0, 0, 0, -1]
// 什么是标准四元数?
// 模长为1的四元数
// 模长 = Mathf.Sqrt(x*x + y*y + z*z * w*w)
// 什么是共轭四元数?
// 虚数部分取负
// 四元数 * 共轭四元数 = 单位四元数
// 什么是四元数的逆?
// q^-1 = q^* / |q| 如果q是标准四元数 q^-1 = q^*
// 如果一个 四元数 * 向量 = 向量 (模长一样,方向不一样,相当于旋转一个向量)
void Start () {
// 绕 (1, 1, 0)轴 旋转45度
result1 = Quaternion.AngleAxis(, new Vector3(, , ));
// 绕 (2, 2, 0)轴 旋转45度
result2 = Quaternion.AngleAxis(, new Vector3(, , )); // 对轴 标准化
Quaternion ge = new Quaternion(-result1.x, -result1.y, -result1.z, result1.w);
result3 = result1 * ge;
Quaternion q = Quaternion.AngleAxis(, Vector3.up);
Vector3 v = new Vector3(, , );
Debug.DrawLine(Vector3.zero, v, Color.green, );
v = q * v;
Debug.DrawLine(Vector3.zero, v, Color.red, );
}
}
RotateAround


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyRotateAroundTest : MonoBehaviour {
public Transform target;
public float rotSpeed = ;
void Start () { } void Update () {
MyRotateAround(target.position, target.up, rotSpeed * Time.deltaTime); }
void MyRotateAround(Vector3 pos, Vector3 axis, float angle)
{
Quaternion q = Quaternion.AngleAxis(angle, axis);
Vector3 dir = transform.position - pos;
Vector3 projectV = (Vector3.Dot(axis, dir) / axis.magnitude) * axis.normalized;
Vector3 n = dir - projectV;
Vector3 newN = q * n;
Vector3 newPos = pos + projectV + newN;
transform.position = newPos;
transform.rotation *= q;
}
}
摄像机

、给Plane和Sphere添加层级
、辅摄像机添加Depth only,Depth改为1,比主摄像机Depth0要大
、主摄像机剔除小球,辅摄像机只保留小球



1方块=1米=100像素
Size = 屏幕分辨率 / (unity默认一个方格的像素) / (屏幕高度的一半)
正好可以把背景投射完整
补充内容-查找篇:

更换天空球的材质

第三人称视角:人
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonalCameraControl : MonoBehaviour {
public float moveSpeed = ;
public float rotateSpeed = ;
private float keyboard_h;
private float keyboard_v;
private Camera camera;
// Use this for initialization
void Start () {
camera = Camera.main;
} // Update is called once per frame
void Update () {
keyboard_h = Input.GetAxis("Horizontal");
keyboard_v = Input.GetAxis("Vertical");
PlayerMove();
}
void PlayerMove()
{
Vector3 cameraForward = Vector3.ProjectOnPlane(camera.transform.forward,Vector3.up).normalized;
Vector3 cameraRight = Vector3.ProjectOnPlane(camera.transform.right, Vector3.up).normalized;
transform.Translate(cameraForward * Time.deltaTime * moveSpeed * keyboard_v);
transform.Translate(cameraRight * Time.deltaTime * moveSpeed * keyboard_h);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdCameraControl : MonoBehaviour {
public Transform player;
public float rotateSpeed = ;
private float mouse_h;
private float mouse_v;
private float mouseWheel;
private Vector3 player2CameraVector;
// Use this for initialization
void Start () {
player2CameraVector = transform.position - player.position;
} // Update is called once per frame
void Update () {
mouse_h = Input.GetAxis("Mouse X") * rotateSpeed;
mouse_v = -Input.GetAxis("Mouse Y") * rotateSpeed;
mouseWheel = Input.GetAxis("Mouse ScrollWheel") * rotateSpeed;
PlayeRotate();
MouseScrollWheel(); }
void PlayeRotate()
{
//一个控制人物(左右转向,放人物身上)
transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed * mouse_h);
Vector3 eulurVector3 = new Vector3(mouse_v, mouse_h, );
Quaternion targetQ = Quaternion.Euler(eulurVector3);
Vector3 newVector3 = targetQ * player2CameraVector;
transform.position = player.position + newVector3;
transform.LookAt(player);
//思考题:上下无法移动
}
void MouseScrollWheel()
{
player2CameraVector = player2CameraVector + player2CameraVector.normalized * mouseWheel;
Vector3 eulurVector3 = new Vector3(mouse_v, mouse_h, );
Quaternion targetQ = Quaternion.Euler(eulurVector3);
Vector3 newVector3 = targetQ * player2CameraVector;
transform.position = player.position + newVector3;
transform.LookAt(player);
//限制
}
}
第一人称视角:人
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstPersonalCameraControl : MonoBehaviour {
public float moveSpeed = ;
public float rotateSpeed = ;
private float keyboard_h;
private float keyboard_v;
private float mouse_h;
private float mouse_v;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () {
keyboard_h = Input.GetAxis("Horizontal");
keyboard_v = Input.GetAxis("Vertical");
mouse_h = Input.GetAxis("Mouse X");
mouse_v = Input.GetAxis("Mouse Y");
PlayerMove();
PlayeRotate();
}
void PlayerMove()
{
//写两个代码:一个控制人物(左右转向,放人物身上),一个控制摄像机(上下转向,放摄像机身上)
transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed * keyboard_v);
transform.Translate(Vector3.right * Time.deltaTime * moveSpeed * keyboard_h);
}
void PlayeRotate()
{
//一个控制人物(左右转向,放人物身上)
transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed * mouse_h);
}
}
第一人称视角:摄像机
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FirstCameraControl : MonoBehaviour {
//上下转动(180)要比左右转动(360)角度小
public float rotateSpeed = ;
private float mouse_v;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () {
mouse_v = Input.GetAxis("Mouse Y");
PlayeRotate();
}
void PlayeRotate()
{
//一个控制摄像机(上下转动,放摄像机身上)
transform.Rotate(Vector3.right * Time.deltaTime * rotateSpeed * -mouse_v);
//限制摄像机上下转动幅度
}
}
汽车
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarMove : MonoBehaviour {
public float moveSpeed = ;
public float rotateSpeed = ;
private float keyboard_h;
private float keyboard_v;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
keyboard_h = Input.GetAxis("Horizontal");
keyboard_v = Input.GetAxis("Vertical");
transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed * keyboard_v);
transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed * keyboard_h);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarCameraController : MonoBehaviour {
public Transform car;
public Transform lookTrans;
public float followTime = 0.5f;
private Vector3 currentVelocity;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = Vector3.SmoothDamp(transform.position, car.position, ref currentVelocity, followTime);
Vector3 dir = lookTrans.position - transform.position;
Quaternion targetQuat = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, targetQuat, 0.1f);
}
}
对象池
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour {
public float moveSpeed = ;
Transform firePos;
private void Awake()
{
firePos = transform.Find("firePos");
}
void Update () {
Vector3 moveDir = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), );
transform.Translate(moveDir * Time.deltaTime * moveSpeed);
if(Input.GetKeyDown(KeyCode.Space))
{
// 通过实例化创建子弹 替换成对象池管理
//Instantiate(bulletPrefab, firePos.position, Quaternion.identity);
PoolManager.Instance.Spawn(firePos.position, Quaternion.identity);//从对象池中取出游戏物体,要记得给对象赋于位置和方向
} }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BossControl : MonoBehaviour {
public int hp = ;
void Start () { } void Update () { }
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Bullet")
{
hp -= ;
// 子弹销毁 替换成对象池管理
//Destroy(other.gameObject);
PoolManager.Instance.Push(other.gameObject);
if(hp <= )
{
Destroy(this.gameObject);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PoolManager : MonoBehaviour
{
private static PoolManager instance = null;
public static PoolManager Instance { get { return instance; } }
public GameObject bulletPrefab;
public List<GameObject> bulletPool = new List<GameObject>();
// 列表来管理
// 创建的时候 从列表中取一个出来 激活
// 销毁的时候 把这个要销毁的游戏物体 失活, 放到列表中
// 把游戏物体 激活、失活
void Awake()
{
instance = this;
}
// 从对象池中取游戏物体
public GameObject Spawn(Vector3 pos, Quaternion qua)
{
GameObject go;
if (bulletPool.Count == )
{
go = Instantiate(bulletPrefab, pos, qua);
}
else
{
go = bulletPool[bulletPool.Count - ];
go.SetActive(true);
bulletPool.RemoveAt(bulletPool.Count - );
go.transform.position = pos;
go.transform.rotation = qua; }
//粒子特效的拖尾问题处理
//ParticleSystem ps = go.GetComponent<ParticleSystem>();
//if (ps) ps.Clear();
//ParticleSystem[] pses = go.GetComponentsInChildren<ParticleSystem>();
//foreach (var item in pses)
//{
// item.Clear();
//}
TrailRenderer tr = go.GetComponent<TrailRenderer>();//TrailRenderer的拖尾问题处理
tr.Clear();
//ps.Play();
//ps.Pause();
return go;
}
// 把游戏物体放回对象池
public void Push(GameObject go)
{
go.SetActive(false);
bulletPool.Add(go);
}
}
Invoke,延时调用函数
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletMove : MonoBehaviour {
public float moveSpeed = ;
void Start () {
// 销毁 替换成对象池管理
//Destroy(this.gameObject, 5f);
Invoke("PushBack", 5f); // 延迟调用 } void Update () {
transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
}
void PushBack()
{
PoolManager.Instance.Push(this.gameObject);
}
}
Unity3D学习笔记(九):摄像机的更多相关文章
- unity3d学习笔记(一) 第一人称视角实现和倒计时实现
unity3d学习笔记(一) 第一人称视角实现和倒计时实现 1. 第一人称视角 (1)让mainCamera和player(视角对象)同步在一起 因为我们的player是生成的,所以不能把mainCa ...
- Unity3D学习笔记2——绘制一个带纹理的面
目录 1. 概述 2. 详论 2.1. 网格(Mesh) 2.1.1. 顶点 2.1.2. 顶点索引 2.2. 材质(Material) 2.2.1. 创建材质 2.2.2. 使用材质 2.3. 光照 ...
- 多线程学习笔记九之ThreadLocal
目录 多线程学习笔记九之ThreadLocal 简介 类结构 源码分析 ThreadLocalMap set(T value) get() remove() 为什么ThreadLocalMap的键是W ...
- MDX导航结构层次:《Microsoft SQL Server 2008 MDX Step by Step》学习笔记九
<Microsoft SQL Server 2008 MDX Step by Step>学习笔记九:导航结构层次 SQL Server 2008中SQL应用系列及BI笔记系列--目录索 ...
- python3.4学习笔记(九) Python GUI桌面应用开发工具选择
python3.4学习笔记(九) Python GUI桌面应用开发工具选择 Python GUI开发工具选择 - WEB开发者http://www.admin10000.com/document/96 ...
- Go语言学习笔记九: 指针
Go语言学习笔记九: 指针 指针的概念是当时学C语言时了解的.Go语言的指针感觉与C语言的没啥不同. 指针定义与使用 指针变量是保存内存地址的变量.其他变量保存的是数值,而指针变量保存的是内存地址.这 ...
- go微服务框架kratos学习笔记九(kratos 全链路追踪 zipkin)
目录 go微服务框架kratos学习笔记九(kratos 全链路追踪 zipkin) zipkin使用demo 数据持久化 go微服务框架kratos学习笔记九(kratos 全链路追踪 zipkin ...
- Unity3D学习笔记3——Unity Shader的初步使用
目录 1. 概述 2. 详论 2.1. 创建材质 2.2. 着色器 2.2.1. 名称 2.2.2. 属性 2.2.3. SubShader 2.2.3.1. 标签(Tags) 2.2.3.2. 渲染 ...
- Unity3D学习笔记4——创建Mesh高级接口
目录 1. 概述 2. 详论 3. 其他 4. 参考 1. 概述 在文章Unity3D学习笔记2--绘制一个带纹理的面中使用代码的方式创建了一个Mesh,不过这套接口在Unity中被称为简单接口.与其 ...
- Unity3D学习笔记6——GPU实例化(1)
目录 1. 概述 2. 详论 3. 参考 1. 概述 在之前的文章中说到,一种材质对应一次绘制调用的指令.即使是这种情况,两个三维物体使用同一种材质,但它们使用的材质参数不一样,那么最终仍然会造成两次 ...
随机推荐
- Java - Spring AOP 拦截器的基本实现
一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...
- 【Espruino】NO.07 获取电压值
http://blog.csdn.net/qwert1213131/article/details/27985645 本文属于个人理解,能力有限,纰漏在所难免.还望指正! [小鱼有点电] 前几节的内容 ...
- beginAppearanceTransition
- (void)beginAppearanceTransition:(BOOL)isAppearing animated:(BOOL)animated __OSX_AVAILABLE_STARTING ...
- 【深入理解javascript】this的用法
引用:this的用法 在函数中this到底取何值,是在函数真正被调用执行的时候确定的,函数定义的时候确定不了 情况1:构造函数 函数作为构造函数用,那么其中的this就代表它即将new出来的对象.另外 ...
- Python高阶函数(Map、Reduce、Filter)
Map函数 map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 举例说明 比如我们有一个函数f(x)=x2,要把这个函数作用 ...
- 在ASP.NET Web Application中通过SOAP协议调用Bing搜索服务
本文介绍了如何在ASP.NET Web Application中将Bing搜索作为Web Service来使用,并通过HTTP的SOAP协议在ASP.NET Web Application中调用Bin ...
- webpack2
中文网址:http://www.css88.com/doc/webpack2/guides/installation/
- 使用 MtVerify.h头文件 ,用的时候把他头文件的内容添加到项目
#include <windows.h> //windodws变量相关头文件 MtVerify.h的内容如下:#pragma comment( lib, "USER32&quo ...
- php hash算法
任意长度的输入, 固定长度的输出 ,该输出就是hash值,这种转换就是一种压缩映射,也就是hash值的空间远远小于输入的空间, 不同的输入可能散列成相同的输出,而不能从hash值来唯一的确定输入值. ...
- class A where T:new()是什么意思
这是C#泛型类声明的语法 class A<T> 表示 A类接受某一种类型,泛型类型为T,需要运行时传入where表明了对类型变量T的约束关系. where T:new()指明了创建T的 ...