[游戏开发-学习笔记]菜鸟慢慢飞(四)-Camera
游戏开发中,主相机应该是最重要的GameObject之一,毕竟游戏呈现给玩家,就是通过它。
相机的使用,在不同的游戏中,有很大的不同。这里总结一下自己学到的一些相关知识。
固定位置-游戏过程中相机的Transform属性不改变。
- 调整好位置后就不要动了,一般使用正交相机,即Camera-Projection选择Orthographic。Unity Manual-Camera
适用:2D游戏。比如飞机大战,消消乐。 - 游戏开始后,相机追踪某一物体,然后固定不动。
游戏开始后,我们才能确定追踪物体的位置,先看代码:
using UnityEngine; public class CameraController : MonoBehaviour
{
public GameObject player;
private Vector3 offset;
void Start ()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
transform.position = player.transform.position + offset;
}
}
适用:固定视角的3D游戏。
3D视角-围绕一个中心随意旋转
先看代码:
using UnityEngine; public class CameraContorller : MonoBehaviour
{
/// <summary>
/// 追踪目标
/// </summary>
public Transform target;
/// <summary>
/// 旋转速度
/// </summary>
public float xSpeed = , ySpeed = ;
/// <summary>
/// 缩放速度
/// </summary>
public float mSpeed = ;
/// <summary>
/// 最小/最大限制角度
/// </summary>
public float yMinLimit = -, yMaxLimit = ;
/// <summary>
/// 是否使用插值运算
/// </summary>
public bool needDamping = true;
/// <summary>
/// 速度
/// </summary>
public float damping = 5.0f; /// <summary>
/// 观察距离
/// </summary>
private float distance = ;
/// <summary>
/// 最小/最大观察距离
/// </summary>
private float minDistance = , maxDistance = ;
/// <summary>
/// 旋转角度
/// </summary>
private float x = 0.0f, y = 0.0f; void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
//根据相机的初始位置初始化距离,最大距离,最小距离;
Vector3 offset = transform.position - target.position;
distance = offset.magnitude;
minDistance = distance / ;
maxDistance = distance * ;
} void LateUpdate()
{
if (target)
{
if (Input.GetMouseButton())//0-左键,1-右键,2-中键
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; y = ClampAngle(y, yMinLimit, yMaxLimit);
}
distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed;
distance = Mathf.Clamp(distance, minDistance, maxDistance); Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
Vector3 disVector = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * disVector + target.position;
if (needDamping)
{
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping);
Vector3 transformTemp = Vector3.Lerp(transform.position, position, Time.deltaTime * damping);
if (transformTemp.y <= )//如果相机位置在“水平面”以下,强制设置到水平面。
{
transform.position = new Vector3(transformTemp.x, , transformTemp.z);
}
else
{
transform.position = transformTemp;
}
}
else
{
transform.rotation = rotation;
if (position.y <= )//如果相机位置在“水平面”以下,强制设置到水平面。
{
transform.position = new Vector3(position.x, , position.z);
}
else
{
transform.position = position;
}
}
}
} static float ClampAngle(float angle, float min, float max)
{
if (angle < -)
angle += ;
if (angle > )
angle -= ;
return Mathf.Clamp(angle, min, max);
}
}
大部分3D游戏中,我这样理解:主角看作球心,球半径玩家可以自己改变,相机的位置就在“地面”以上的半球球体上。
上面这段代码基本做到了这个要求,并在相机到“地面”的时候有抬头看的效果。
代码有点问题,开始游戏后相机会跳动两次,暂时放到自己的问题清单中。
其他用法
官方教程Tanks tutorial 中有个不常见的用法。
不管两个Tank如何移动,相机通过前后缩放,大小缩放保证两个Tank肯定在相机视野内。
主要代码:(官方代码写得好啊)
private void Move ()
{
// Find the average position of the targets.
FindAveragePosition (); // Smoothly transition to that position.
transform.position = Vector3.SmoothDamp(transform.position, m_DesiredPosition, ref m_MoveVelocity, m_DampTime);
} private void FindAveragePosition ()
{
Vector3 averagePos = new Vector3 ();
int numTargets = ; // Go through all the targets and add their positions together.
for (int i = ; i < m_Targets.Length; i++)
{
// If the target isn't active, go on to the next one.
if (!m_Targets[i].gameObject.activeSelf)
continue; // Add to the average and increment the number of targets in the average.
averagePos += m_Targets[i].position;
numTargets++;
} // If there are targets divide the sum of the positions by the number of them to find the average.
if (numTargets > )
averagePos /= numTargets; // Keep the same y value.
averagePos.y = transform.position.y; // The desired position is the average position;
m_DesiredPosition = averagePos;
} private void Zoom ()
{
// Find the required size based on the desired position and smoothly transition to that size.
float requiredSize = FindRequiredSize();
m_Camera.orthographicSize = Mathf.SmoothDamp (m_Camera.orthographicSize, requiredSize, ref m_ZoomSpeed, m_DampTime);
} private float FindRequiredSize ()
{
// Find the position the camera rig is moving towards in its local space.
Vector3 desiredLocalPos = transform.InverseTransformPoint(m_DesiredPosition); // Start the camera's size calculation at zero.
float size = 0f; // Go through all the targets...
for (int i = ; i < m_Targets.Length; i++)
{
// ... and if they aren't active continue on to the next target.
if (!m_Targets[i].gameObject.activeSelf)
continue; // Otherwise, find the position of the target in the camera's local space.
Vector3 targetLocalPos = transform.InverseTransformPoint(m_Targets[i].position); // Find the position of the target from the desired position of the camera's local space.
Vector3 desiredPosToTarget = targetLocalPos - desiredLocalPos; // Choose the largest out of the current size and the distance of the tank 'up' or 'down' from the camera.
size = Mathf.Max(size, Mathf.Abs(desiredPosToTarget.y)); // Choose the largest out of the current size and the calculated size based on the tank being to the left or right of the camera.
size = Mathf.Max(size, Mathf.Abs(desiredPosToTarget.x) / m_Camera.aspect);
} // Add the edge buffer to the size.
size += m_ScreenEdgeBuffer; // Make sure the camera's size isn't below the minimum.
size = Mathf.Max (size, m_MinSize); return size;
}
暂时放下的内容
Unity的相机相关:
- Screen Space Ambient Obscurance
- Bloom
- TiltShift
- VignetteAndChromaticAberration
- Color Correction Curves
[游戏开发-学习笔记]菜鸟慢慢飞(四)-Camera的更多相关文章
- [游戏开发-学习笔记]菜鸟慢慢飞(九)- NGUI- UIPanel(官方说明翻译)
我自己笔记是做在OneNote上,直接复制粘贴过来变成图片了,效果好像还可以. 机器翻译,我自己看了一下,改了一部分.
- [游戏开发-学习笔记]菜鸟慢慢飞(九)- NGUI- UIWidget(官方说明翻译)
- 【整理】HTML5游戏开发学习笔记(5)- 猜谜游戏
距上次学习笔记已有一个多月过去了,期间由于新项目赶进度,以致该学习计划给打断,十分惭愧.书本中的第六章的例子相对比较简单.所以很快就完成. 1.预备知识html5中video标签的熟悉 2.实现思路对 ...
- 【整理】HTML5游戏开发学习笔记(1)- 骰子游戏
<HTML5游戏开发>,该书出版于2011年,似乎有些老,可对于我这样没有开发过游戏的人来说,却比较有吸引力,选择自己感兴趣的方向来学习html5,css3,相信会事半功倍.不过值得注意的 ...
- cocos2d-x 3.x游戏开发学习笔记(1)--mac下配置cocos2d-x 3.x开发环境
打开用户文件夹下.bash_profile文件,配置环境 vim ~/.bash_profile //按键i,进行插入编辑(假设输错d进行删除一行) 环境配置过程例如以下: 1.首先配置下androi ...
- Photon + Unity3D 线上游戏开发 学习笔记(四)
这一节 我们建立 photon Server 端的框架 一个最简单的Photon框架 就包括一个 Applocation 类 和 一个 peer 类,作用例如以下: * Application 类是 ...
- [Android游戏开发学习笔记]View和SurfaceView
本文为阅读http://blog.csdn.net/xiaominghimi/article/details/6089594的笔记. 在Android游戏中充当主要角色的,除了控制类就是显示类.而在A ...
- Photon + Unity3D 线上游戏开发 学习笔记(一)
大家好. 我也是学习Photon + unity3D 的新手 有什么说错的地方大家见谅哈. 我的开发环境是 unity3D 4.1.3 , Visual Studio 是2010 版本号的 p ...
- 【Android开发学习笔记】【第四课】基础控件的学习
通过一个简单的例子来学习下面几种控件: 1.TextView:简单的文本显示控件 2.EditText:可以编辑的文本框 3.Button:按钮 4.Menu:这里指的是系统的Menu 5.Toast ...
随机推荐
- JavaScript语言精粹--Function,类,this,对象
1.类与对象 在JS中,创建对象(Create Object)并不完全是我们时常说的创建类对象,JS中的对象强调的是一种复合类型,JS中创建对象及对对象的访问是极其灵活的. JS对象是一种复合类型,它 ...
- CRL快速开发框架升级到3.1
CRL是一款面向对象的轻量级ORM框架,本着快速开发,使用简便的原则,设计为 无需关心数据库结构,CRL自动维护创建,即写即用(CRL内部有表结构检查机制,保证表结构一致性) 无需第三方工具生成代理类 ...
- 修改版: 小伙,多线程(GCD)看我就够了,骗你没好处!
多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提升整体处理性能.具有这种能力的系 ...
- 微信小程序的应用及信息整合,都放到这里了
微信小程序终于开始公测了,这篇文章也终于可以发布了. 这篇文章可以说是微信小程序系列三部曲最后一篇.8 月份,小程序推出前,我写了<别开发 app 了>详细阐述了为什么创业应该放弃原生 a ...
- docker4dotnet #5 使用VSTS/TFS搭建基于容器的持续交付管道
在过去的几篇d4d系列中,我给大家介绍了如何使用docker来支持asp.net core的应用开发,打包的场景.Asp.net core的跨平台开发能力为.net开发人员提供了使用容器进行应用开发的 ...
- 联想 Thinkpad X230 SLIC 2.1 Marker
等了好久,终于等到了 X230 的 SLIC 2.1 的 Marker !特发帖备份... 基本情况 笔记本:Lenovo X230(i5+8G+500G) 操作系统:Windows 7 Pro x6 ...
- IOS学习之-私人通讯录
通过一段时间IOS的学习完成了一个简单的应用,"私人通讯录". 运行效果如下图: 1.登录页 2.通讯录列表 3.添加 4.编辑 5.删除 6.注销 总视图结构如下图: 总结本程序 ...
- 前端开发css实战:使用css制作网页中的多级菜单
前端开发css实战:使用css制作网页中的多级菜单 在日常工作中,大家都会遇到一些显示隐藏类菜单,比如页头导航.二维码显示隐藏.文本提示等等......而这些效果都是可以使用纯css实现的(而且非常简 ...
- 文字处理控件TX Text Control的使用
这几天一直在研究TX Text Control的使用,由于这方面的资料相对比较少,主要靠下载版本的案例代码进行研究,以及官方的一些博客案例进行学习,使用总结了一些心得,特将其总结出来,供大家分享学习. ...
- ef
现阶段使用回溯 entityframework作为.net平台自己的一个orm的框架,之前在项目中也有使用,主要采用了table和model first的方式,此两种感觉使用上也是大同小异.在项目中经 ...