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. 概述 在之前的文章中说到,一种材质对应一次绘制调用的指令.即使是这种情况,两个三维物体使用同一种材质,但它们使用的材质参数不一样,那么最终仍然会造成两次 ...
随机推荐
- HashMap(不是线程安全)与ConcurrentHashMap(线程安全)
HashMap不是线程安全的 ConcurrentHashMap是线程安全的 从JDK1.2起,就有了HashMap,正如前一篇文章所说,HashMap不是线程安全的,因此多线程操作时需要格外小心. ...
- [LeetCode] 394. Decode String_Medium tag: stack 666
Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...
- Leetcode: Construct Binary Tree from Preorder and Inorder Transversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...
- js值类型和引用类型的区别
1:赋值 值类型的赋值:直接将存储的数据赋值一份进行赋值,两份数据在内存中是完全独立的. 引用类型赋值:引用类型的赋值的时候,是将变量中的存储的地址赋值一份单独存储,但是两个变量中修改其中一个对象,另 ...
- 认识GMT和UTC时间-附带地理知识
GMT-格林尼治标准时 GMT 的全名是格林威治标准时间或格林威治平时 (Greenwich Mean Time),这个时间系统的概念在 1884 年确立,由英国伦敦的格林威治皇家天文台计算并维护,并 ...
- 网页中自适应的显示PDF
PDF格式呢,是一个高大的新式,如何在不同的浏览器中自适应显示,是一个值得研究的问题. 这里说明重点部分:获取浏览器宽高. IE中: document.body.clientWidth ==> ...
- Bootstrap3-文字样式
Bootstrap将全局font-size设置为14px,line-height为1.428.这些属性直接赋给<body>和所有段落元素.另外,<p>(段落)还被设置了等于1/ ...
- Python: Pycharm简单介绍
1. Pycharm是什么? ...
- OLAP引擎——Kylin介绍(很有用)
转:http://blog.csdn.net/yu616568/article/details/48103415 Kylin是ebay开发的一套OLAP系统,与Mondrian不同的是,它是一个MOL ...
- linux centos系统下升级python版本
本文参考资料:https://www.cnblogs.com/leon-zyl/p/8422699.html,https://blog.csdn.net/tpc1990519/article/deta ...