【Unity3D】相机
1 简介
相机用于渲染游戏对象,每个场景中可以有多个相机,每个相机独立成像,每个成像都是一个图层,最后渲染的图层在最前面显示。
相机的属性面板如下:

- Clear Flags:设置清屏颜色,Skybox(天空盒)、Solid Color(纯色)、Depth Only(仅深度,画中画效果)、Don't Clear(不渲染);
- Background:清屏颜色,当 Clear Flags 为 Skybox 或 Solid Color 时,才有 Background;
- Culling Mask:剔除遮蔽,选择相机需要绘制的对象层;
- Projection:投影方式,Perspective(透视投影)、Orthographic(正交投影);
- Field of View:相机视野范围,默认 60°,视野范围越大,能看到的物体越多,看到的物体越小;
- Clipping Planes:裁剪平面,视锥体中近平面和远平面的位置;
- Viewport Rect:视口在屏幕中的位置和大小(相对值),以屏幕左下角为原点;
- Depth:渲染图层深度,用于设置相机渲染图层的显示顺序;
- Rendering Path:渲染路径,用于调整渲染性能(可以在 Stats 里查看性能)
- Target Texture:将相机画面存储到一个纹理图片(RenderTexture)中。
补充:基于 Target Texture 属性,可以使用 RawImage 显示次相机渲染的 RenderTexture,实现带主题边框的小地图、八倍镜等效果。
2 应用
本节将通过实现小地图案例展示相机的应用。
1)游戏界面

2)游戏对象层级结构

3)Transform 组件参数
1. 相机 Transform 组件参数
| Name | Type | Position | Rotation | Scale | Viewport Rect |
|---|---|---|---|---|---|
| Main Camera | Camera | (0, 4, -5) | (38, 0, 0) | (1, 1, 1) | (0, 0, 1, 1) |
| MinimapCamera | Camera | (0, 12, 0) | (90, 0, 0) | (1, 1, 1) | (0.8, 0.7, 0.2, 0.3) |
2. 玩家 Transform 组件参数
| Name | Type | Position | Rotation | Scale | Color/Texture |
|---|---|---|---|---|---|
| Player | Empty | (0, 0.25, -5) | (0, 0, 0) | (1, 1, 1) | #228439FF |
| Botton | Cube | (0, 0, 0) | (0, 0, 0) | (2, 0.5, 2) | #228439FF |
| Top | Cube | (0, 0.5, 0) | (0, 0, 0) | (1, 0.5, 1) | #228439FF |
| Gun | Cylinder | (0, 0, 1.5) | (90, 0, 0) | (0.2, 1, 0.4) | #228439FF |
| FirePoint | Empty | (0, 1.15, 0) | (0, 0, 0) | (1, 1, 1) | —— |
补充:Player 游戏对象添加了刚体组件。
3. 敌人 Transform 组件参数
| Name | Type | Position | Rotation | Scale | Color/Texture |
|---|---|---|---|---|---|
| Enemy1 | Empty | (7, 0.25, 1) | (0, 120, 0) | (1, 1, 1) | #F83333FF |
| Enemy2 | Enemy | (-1, 0.25, 5) | (0, 60, 0) | (1, 1, 1) | #EA9132FF |
| Enemy3 | Enemy | (-5, 0.25, -1) | (0, -40, 0) | (1, 1, 1) | #5DC2F4FF |
说明:Enemy1~Enemy3 都是由 Player copy 而来。
4. 地面和炮弹 Transform 组件参数
| Name | Type | Position | Rotation | Scale | Color/Texture |
|---|---|---|---|---|---|
| Plane | Plane | (0, 0, 0) | (0, 0, 0) | (10, 10, 10) | GrassRockyAlbedo |
| Bullet | Sphere | (0, 0.5, -5) | (0, 0, 0) | (0.3, 0.3, 0.3) | #228439FF |
补充:炮弹作为预设体拖拽到 Assets/Resources/Prefabs 目录下,并且添加了刚体组件。
4)脚本组件
MainCameraController.cs
using UnityEngine;
public class MainCameraController : MonoBehaviour {
private Transform player; // 玩家
private Vector3 relaPlayerPos; // 相机在玩家坐标系中的位置
private float targetDistance = 15f; // 相机看向玩家前方的位置
private void Start() {
relaPlayerPos = new Vector3(0, 4, -8);
player = GameObject.Find("Player/Top").transform;
}
private void LateUpdate() {
CompCameraPos();
}
private void CompCameraPos() { // 计算相机坐标
Vector3 target = player.position + player.forward * targetDistance;
transform.position = transformVecter(relaPlayerPos, player.position, player.right, player.up, player.forward);
transform.rotation = Quaternion.LookRotation(target - transform.position);
}
// 求以origin为原点, locX, locY, locZ 为坐标轴的本地坐标系中的向量 vec 在世界坐标系中对应的向量
private Vector3 transformVecter(Vector3 vec, Vector3 origin, Vector3 locX, Vector3 locY, Vector3 locZ) {
return vec.x * locX + vec.y * locY + vec.z * locZ + origin;
}
}
说明: CameraController 脚本组件挂在 MainCamera 游戏对象上。
MinmapCameraController.cs
using UnityEngine;
public class MinimapController : MonoBehaviour {
private Transform player; // 玩家
private float height = 12; // 相机离地面的高度
private bool isFullscreen = false; // 小地图相机是否全屏
private Rect minViewport; // 小地图视口位置和大小(相对值)
private Rect FullViewport; // 全屏时视口位置和大小(相对值)
private void Start() {
player = GameObject.Find("Player/Top").transform;
minViewport = GetComponent<Camera>().rect;
FullViewport = new Rect(0, 0, 1, 1);
}
private void Update() {
if (Input.GetMouseButtonDown(0) && IsClickMinimap()) {
if (isFullscreen) {
GetComponent<Camera>().rect = minViewport;
} else {
GetComponent<Camera>().rect = FullViewport;
}
isFullscreen = !isFullscreen;
}
}
private void LateUpdate() {
Vector3 pos = player.position;
transform.position = new Vector3(pos.x, height, pos.z);
}
public bool IsClickMinimap() { // 是否单击到小地图区域
Vector3 pos = Input.mousePosition;
if (isFullscreen) {
return true;
}
if (pos.x / Screen.width > minViewport.xMin && pos.y / Screen.height > minViewport.yMin) {
return true;
}
return false;
}
}
说明: MinimapController 脚本组件挂在 MinimapCamera 游戏对象上。
PlayerController.cs
using UnityEngine;
public class PlayerController : MonoBehaviour {
private MinimapController minimapController; // 小地图控制器
private Transform firePoint; // 开火点
private GameObject bulletPrefab; // 炮弹预设体
private float tankMoveSpeed = 4f; // 坦克移动速度
private float tankRotateSpeed = 2f; // 坦克转向速度
private Vector3 predownMousePoint; // 鼠标按下时的位置
private Vector3 currMousePoint; // 当前鼠标位置
private float fireWaitTime = float.MaxValue; // 距离上次开火已等待的时间
private float bulletCoolTime = 0.15f; // 炮弹冷却时间
private void Start() {
minimapController = GameObject.Find("MinimapCamera").GetComponent<MinimapController>();
firePoint = transform.Find("Top/Gun/FirePoint");
bulletPrefab = (GameObject) Resources.Load("Prefabs/Bullet");
}
private void Update() {
Move();
Rotate();
Fire();
}
private void Move() { // 坦克移动
float hor = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
if (Mathf.Abs(hor) > float.Epsilon || Mathf.Abs(ver) > float.Epsilon) {
Vector3 vec = transform.forward * ver + transform.right * hor;
GetComponent<Rigidbody>().velocity = vec * tankMoveSpeed;
Vector3 dire = new Vector3(hor, ver, 0);
dire = dire.normalized * Mathf.Min(dire.magnitude, 1);
}
}
private void Rotate() { // 坦克旋转
if (Input.GetMouseButtonDown(1)) {
predownMousePoint = Input.mousePosition;
} else if (Input.GetMouseButton(1)) {
currMousePoint = Input.mousePosition;
Vector3 vec = currMousePoint - predownMousePoint;
GetComponent<Rigidbody>().angularVelocity = Vector3.up * tankRotateSpeed * vec.x;
predownMousePoint = currMousePoint;
}
}
private void Fire() { // 坦克开炮
fireWaitTime += Time.deltaTime;
if (Input.GetMouseButtonDown(0) && !minimapController.IsClickMinimap() || Input.GetKeyDown(KeyCode.Space)) {
if (fireWaitTime > bulletCoolTime) {
BulletInfo bulletInfo = new BulletInfo("PlayerBullet", Color.red, transform.forward, 10f, 15f);
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, Quaternion.identity);
bullet.AddComponent<BulletController>().SetBulletInfo(bulletInfo);
fireWaitTime = 0f;
}
}
}
}
说明: PlayerController 脚本组件挂在 Player 游戏对象上。
BulletController.cs
using UnityEngine;
using UnityEngine.UI;
public class BulletController : MonoBehaviour {
private BulletInfo bulletInfo; // 炮弹信息
private void Start () {
gameObject.name = bulletInfo.name;
GetComponent<MeshRenderer>().material.color = bulletInfo.color;
float lifeTime = bulletInfo.fireRange / bulletInfo.speed; // 存活时间
Destroy(gameObject, lifeTime);
}
private void Update () {
transform.GetComponent<Rigidbody>().velocity = bulletInfo.flyDir * bulletInfo.speed;
}
public void SetBulletInfo(BulletInfo bulletInfo) {
this.bulletInfo = bulletInfo;
}
}
说明:BulletController 脚本组件挂在 Bullet 游戏对象上(代码里动态添加)。
BulletInfo.cs
using UnityEngine;
public class BulletInfo {
public string name; // 炮弹名
public Color color; // 炮弹颜色
public Vector3 flyDir; // 炮弹飞出方向
public float speed; // 炮弹飞行速度
public float fireRange; // 炮弹射程
public BulletInfo(string name, Color color, Vector3 flyDir, float speed, float fireRange) {
this.name = name;
this.color = color;
this.flyDir = flyDir;
this.speed = speed;
this.fireRange = fireRange;
}
}
5)运行效果

声明:本文转自【Unity3D】相机
【Unity3D】相机的更多相关文章
- Unity3D 相机跟随主角移动
这里给主相机绑定一个脚本. 脚本写为: using UnityEngine; using System.Collections; public class camerafollow : MonoBeh ...
- Unity3D相机震动效果
在一些格斗.射击以及动作类游戏中 相机震动效果是十分重要的 一个平凡的镜头 在关键时刻加入相机震动后 便可以展现出碰撞.危险.打斗以及激动人心的效果 相机震动的实现方式有很多 但都会涉及摄像机位置的变 ...
- Unity3D——相机跟随物体移动
public Transform target; public float moveSmooth=5f; Vector3 offset; void Start () { offset = transf ...
- [原]unity3D 相机跟随
using UnityEngine;using System.Collections; public class CameraFollow : MonoBehaviour { p ...
- [Unity3D]Unity资料大全免费分享
都是网上找的连七八糟的资料了,整理好分享的,有学习资料,视频,源码,插件……等等 东西比较多,不是所有的都是你需要的,可以按 ctrl+F 来搜索你要的东西,如果有广告,不用理会,关掉就可以了,如 ...
- 分享一个大神自己的blog
std::sort() 详解 http://feihu.me/blog/ C++11 新特性 http://blog.guoyb.com/2016/09/19/cpp11-all/ unity3d 相 ...
- Unity3d学习 相机的跟随
最近在写关于相机跟随的逻辑,其实最早接触相机跟随是在Unity官网的一个叫Roll-a-ball tutorial上,其中简单的涉及了关于相机如何跟随物体的移动而移动,如下代码: using Unit ...
- [Unity3D]深度相机 Depth Camera
作为3D世界里最重要的窗口,摄像机的应用就显得很重要,毕竟在屏幕上看到的一切都得用摄像机矩阵变换得来的嘛.论坛上看到了一篇帖子讲非天空盒的背景做法,让我想起其实很多界面合成画面可以用摄像机之间的交互来 ...
- unity3d 第三人称视角的人物移动以及相机控制
何谓第三人称?就像这样: 用wasd控制人物移动,同时保持在相机的中心.用鼠标右键与滚轮控制相机的角度和距离. 先说一下人物的移动: 首先给作为主角的单位加上 Charactor Controller ...
- unity3d设置3D模型显示在2D背景之前(多个相机分层显示)(转)
解决步骤: 1.添加一个摄像机,命名为BackgroundCamera,然后在Layer添加一个background层.并且将plane拖放到改相机节点下. 然后将BackgroundCamera和P ...
随机推荐
- [转帖]技术分享| MySQL 的 AWR Report?— MySQL 状态诊断报告
https://segmentfault.com/a/1190000039959767 作者:秦福朗 爱可生 DBA 团队成员,负责项目日常问题处理及公司平台问题排查.热爱 IT,喜欢在互联网 ...
- [转帖]容器环境的JVM内存设置最佳实践
https://cloud.tencent.com/developer/article/1585288 Docker和K8S的兴起,很多服务已经运行在容器环境,对于java程序,JVM设置是一个重要的 ...
- [转帖]十步解析awr报告
http://www.zhaibibei.cn/awr/1.1/ 从这期开始讲解awr报告的部分,首先讲解awr整体的部分 后续会针对不同的点进行讲解 1. 数据库细节 这部分可以看到 数据库的版本 ...
- Windows平台文件拆分与完整性检查的过程
Windows平台文件拆分与完整性检查的过程 场景 有时候在没有linux主机的情况下, 自己下载下来的文件比较大. 比较难以上传到一些特殊的系统/主机上面. 这个时候需要将文件进行拆分. 所以可以通 ...
- [转帖]Veeam Backup & Replication 10.0.0.4461安装部署(包含补丁)
原文:https://www.cnblogs.com/cnzay/p/15561893.html Veeam Backup & Replication 是一款数据保护软件,为VMware 和H ...
- XJTU少年班+自动化钱学森班+电气工程辅修专业课笔记合集
通过百度网盘分享的文件:笔记整理链接:https://pan.baidu.com/s/1BrHQ1EqvlQlbWqpD5h_6Sg?pwd=shsg 提取码:shsg复制这段内容打开「百度网盘APP ...
- git有关commit的命令
2.更改最近一次(本次) commit 的提交信息: 当我们执行 git add . git commit -m "0-0-1" 之后我们发现自己写的提交信息是不符合项目要求的,这 ...
- git日志输出相关命令
git log 默认输出所有的日志 git log 默认输出所有的日志 git 日志输出--只看最近的两条或者三条 有些时候我们可能只需要看最近的2或者3条日志 git log -2 日志输出--只看 ...
- ES6 Array.fiill()的用法
简单使用 // arr.fill(value, start, end) // value填充的值 // start填充的起始位置包含 // end填充的结束值,不包含,如果省略这个参数,表示从起始位置 ...
- 使用Java读取Excel文件数据
通过编程方式读取Excel数据能实现数据导入.批量处理.数据比对和更新等任务的自动化.这不仅可以提高工作效率还能减少手动处理的错误风险.此外读取的Excel数据可以与其他系统进行交互或集成,实现数据的 ...