二十、摄像机
大纲:红色为必须掌握内容
Clear Flags 背景类型:
英语生词:solid:纯色的。
其中
skybox:管理天空画面一般用于3D。
Solid Color:颜色填充用于2D。
Depth only:通常用于多个摄像机调整culling mask来渲染不同层级。
Don't Clear: 不常用,可以在摄像机留下画面。
Culling Mask 剔除遮罩
英语生词:Culling:筛选
用于控制摄像机只渲染对应层级些。
Projection 投影
英语生词:Perspective:透视。Orthographic:正交。
透视模式具有近大远小和3D视觉效果。而正交模式是没有的。
Clipping Planes 裁剪平面:
英语生词:Clipping:裁剪。Planes:平面。
用来控制渲染开始平面距离相机对象距离和可以渲染多远。
Physical Camera 物理相机
点击后才可出现,用来模拟特殊相机视角
Depth 优先级
用来和剔除层级,多个摄像机来进行操作。值越小越先渲染,但会被大值相机渲染画面遮挡。
层级大的设置剔除遮罩,并将环境选择为Depth only即可体验多个摄像机
Target Texture 输出纹理
一般用于创建小地图,需要创建渲染器纹理(Render Texture)
Occlusion Culling 遮挡剔除
用于被遮挡物体不渲染,节约性能
Viewport 矩形
用于调整摄像机窗口大小和位置在画面中
二十一、摄像机脚本相关
重要静态成员
获取摄像机:
//获取主摄像机,需要场景中有标签为MainCamera的摄像机。若有多个摄像机标签为主摄像机,则会获取其中一个。
print(Camera.main.name);
//获取所有摄像机数量(已激活的)
print(Camera.allCamerasCount);
//获取所有摄像机对象(已激活的)
Camera[] cs = Camera.allCameras;
print(cs.Length);
渲染相关委托
重要成员
获取属性可以通过摄像机获取属性
世界坐标转屏幕坐标
屏幕坐标转屏幕坐标
练习题
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class L_CameraScript1 : MonoBehaviour
{
public GameObject go;
// Update is called once per frame
void Update()
{
Vector3 v = Camera.main.WorldToScreenPoint(go.transform.position);
print(v);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class L_CameraScript1 : MonoBehaviour
{
// Update is called once per frame
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 camPos = Input.mousePosition;
camPos.z = 10;
camPos = Camera.main.ScreenToWorldPoint(camPos);
GameObject go1 = GameObject.CreatePrimitive(PrimitiveType.Cube);
go1.transform.position = camPos;
}
}
}
二十二、光源组件
光源类型
颜色、模式、光源亮度
阴影
生硬阴影消耗比柔和小,但会出现锯齿
cookie 投影遮罩
用于改变投影的光图案。
阴影效果
二十三、光面板相关
环境相关设置
其他设置
二十四、碰撞检测-刚体
碰撞产生条件:
属性讲解:
英语生词:Drag:阻力。Mass:质量。Angular:角。gravity:重力。
质量、空气阻力、角阻力、是否受重力影响
Drag是空气阻力影响的速度,而Angular Drag是影响旋转速度。
是否运动
英语生词:Kinematic:运动。
插值
英语生词:Interpolate:插入
用于物理帧调整时。
碰撞检测模式
主要看两种物体碰撞所使用的检测模式。
选择停止移动
二十五、碰撞器
碰撞器种类
碰撞器共同参数
特殊碰撞器参数
异型物体使用多种碰撞器组合
可以使用在父对象上添加刚体来实现,子对象添加碰撞器。
不常用的碰撞器
英语生词:Convex:凸多边形。
网格碰撞器:拥有刚体的物体需要开启,否则报错。
二十六、碰撞检测-物理材质
创建物理材质
动态和静态摩擦力 Dynamic 和 Static Friction
英语生词:Dynamic:动态。 friction:摩擦力。
用来模拟摩擦力系数。
弹力 Bounciness
Bounciness:弹力。
摩擦力与弹力组合关系
二十七、碰撞检查-碰撞检测函数
有触发器响应函数和碰撞检查响应函数两种,他们都在物理帧更新之后触发,间隔时间由物理帧间隔时间控制。
他们都是特殊的生命周期函数,都是通过反射去调用。
碰撞检测响应函数
碰撞接触触发、分离触发、互相摩擦一直触发:
参数Conllision:
触发器响应函数
需要有点击 碰撞器 - IsTrigger 才能触发。
该触发器响应函数用于没有物理效果,但需要触发函数的情况。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class L14 : MonoBehaviour
{
//第一次相交(相互触碰到)
private void OnTriggerEnter(Collider other)
{
print($"与{other.name}相交了");
}
//离开
private void OnTriggerExit(Collider other)
{
print($"与{other.name}相离了");
}
//相互重叠,相交
private void OnTriggerStay(Collider other)
{
print($"与{other.name}正在相容中");
}
}
其他提示:
练习题:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tank : MonoBehaviour
{
public int removeSpeed;//前后移动速度
public int rotateSpeed;//车体左右旋转速度
public int selfRotateSpeed;//炮塔旋转速度
public int gunUpDownSpeed;//炮管上下速度
public GameObject go;//炮塔
public Transform EmptyTransform;//炮管上下围绕点
public GameObject gun;//炮管
public GameObject camera1;//摄像机
public GameObject muzzle;//枪口:用于发射子弹
public GameObject zidan;
Vector3 cameraOffset;//相机和坦克的距离向量
// Update is called once per frame
private void Start()
{
camera1.transform.LookAt(this.transform.position);
cameraOffset = camera1.transform.position - this.transform.position;
}
void Update()
{
//W、S控制前进后退采用 公式为:正方向 * 速度 * 时间 * 输入:向哪边运动 传入的是一个方向向量,默认坐标系是Space.Self
this.transform.Translate(Vector3.forward * removeSpeed * Time.deltaTime * Input.GetAxis("Vertical"));
//A、D控制左右旋转车体 公式为:旋转轴 * 速度 * 时间 * 输入
this.transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime * Input.GetAxis("Horizontal"));
//鼠标左右移动控制炮台旋转 公式为:旋转轴 * 速度 * 时间 * 输入
go.transform.Rotate(Vector3.up * selfRotateSpeed * Time.deltaTime * Input.GetAxis("Mouse X"));
//鼠标滑轮控制炮管上下 参数:哪一个点:Vector3 point, 哪个轴:Vector3 axis, 多少度:float angle
gun.transform.RotateAround(EmptyTransform.position, Vector3.left, gunUpDownSpeed * Time.deltaTime * Input.mouseScrollDelta.y);
//鼠标右键进入->鼠标移动控制摄像机绕车旋转,并看向车
if (Input.GetMouseButton(1))
{
print("右键进入");
print(Input.GetAxis("Mouse X"));
camera1.transform.RotateAround(this.transform.position, Vector3.up, 200 * Time.deltaTime * Input.GetAxis("Mouse X"));
//camera1.transform.RotateAround(this.transform.position, Vector3.left, 200 * Time.deltaTime * Input.GetAxis("Mouse Y"));
cameraOffset = camera1.transform.position - this.transform.position;
}
if (Input.GetMouseButtonDown(0))
{
GameObject mu = Instantiate(zidan);
mu.transform.position = muzzle.transform.position;
mu.transform.rotation = muzzle.transform.rotation;
//mu.transform.rotation = muzzle.transform.rotation;
Rigidbody rb = mu.GetComponent<Rigidbody>();
if (rb == null)
{
rb = mu.AddComponent<Rigidbody>();
}
rb.useGravity = false;
rb.velocity = mu.transform.forward * 0.1f;
}
camera1.transform.position = this.transform.position + cameraOffset;
}
}
二十七、刚体加力
添加刚体组件及加力
添加扭矩力
改变速度
添加爆炸效果
参数:爆炸力、爆炸点、爆炸范围
力的模式
动量定理:
四种模式(具体)
力场脚本(组件)
刚体休眠解决
二十八、音频
常用音频格式:
音频检视窗口下方三个按钮:
分别为:播放/暂停;自动播放;循环播放。
具体属性:
音频脚本
需要添加Audio Source脚本组件,其中的项为:
代码控制音频
延迟播放:
检测是否播放完毕:
动态控制:
麦克风:
获取设备:
开始录制:
结束录制:
获取音频数据用于存储或传输: