部分内容由千峰教育(莫新宇)听课笔记总结
Properties
Is Trigger If enabled, this Collider is used for triggering events, and is ignored by the physics engine.
Material Reference to the Physics Material that determines how this Collider interacts with others.
Center The position of the Collider in the object's local space.
Size The size of the Collider in the X, Y, Z directions.
碰撞器
碰撞体组件的作用
1.用一个包围盒(box,sphere, capsule)将一个模型包裹起来,该包围盒来秒速碰撞范围
2.配合刚体组件,实现物理碰撞效果
Collider组件
碰撞盒编辑 Edit Collider
是否设置为触发器
添加物理材质
碰撞体大小中心设置
Collider种类
Box
Sphere
Capsule
Mesh 使用时减少顶点和面
绿色包围盒
编辑Collider碰撞体
实际效果:
游戏范例:空气墙的做法
Material: 物理材质、摩擦力和弹力
碰撞体由三角形组成
isTrigger is the collider a trigger?
Agular Drag拐弯时受到的阻力
Is Kinematic 受到施加的力,不会发生移动
Interpolate 插值,增加动画帧数
Collision detection
Discrete/Continuous/Continuous Dynamic
刚体的一些参数
常用方法
AddForce(Vector3) 添加一个力
AddExplosionForce 添加爆炸力 参数列表:爆炸力大小Flost、 爆炸点Vector3、 爆炸半径Flost
注意:执行力添加爆炸力的方法,才能产生爆炸,不执行方法,即便距离爆炸点很近,也不会有任何爆炸效果
AddForcePosition 在一个点上添加一个力
AddRelativeForce 添加相对力
AddRelativeTorque 添加相对力矩
using UnityEngine;
public class RigidbodyClass : MonoBehaviour {
private Rigidbody rig;
// Use this for initialization
void Start () {
rig = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
Debug.Log(rig.velocity);
if (Input.GetKey(KeyCode.Space)) { rig.velocity = Vector3.up * 20; }
transform.position += Vector3.right * Time.deltaTime * 1;
}
}
using UnityEngine;
public class RigidbodyClass : MonoBehaviour {
private Rigidbody rig;
// Use this for initialization
void Start () {
rig = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
//向前施加一个力
if (Input.GetKey(KeyCode.F)) { rig.AddForce(Vector3.forward*100); }
//施加一个爆炸力
if (Input.GetKey(KeyCode.E)) { rig.AddExplosionForce(100,transform.position,3); }
}
}
碰撞检测
碰撞产生的条件
两物体都有Collider
至少有一个带有RigidBody
两物体保持相对运动
回调方法
OnCollisionEnter(Collision other) 碰撞开始一瞬间时调用一次
OnCollisionStay(Collision other) 碰撞过程中相对运动时持续调用,每帧一次
OnCollisionExit(Collision other) 碰撞分离一瞬间调用
回调方法参数
collider 当前物体所碰撞到的游戏对象(游戏对象的碰撞体组件)
contacts[] 所有碰撞点的信息(normal, point(碰撞点的坐标)) Vector3 contacts[0]表示第一个接触的点,其他依此类推
触发事件
触发事件产生的条件
两物体都有碰撞体
至少有一个带有刚体
两碰撞体至少有一个是触发器
设置触发器
将碰撞体组件中的isTrigger属性构选
回调函数
OnTriggerEnter(Collider other) 进入触发器的时候调用一次
OnTriggerStay(Collider other) 处于某个触发器之中的时候持续调用,每帧一次
OnTriggerExit(Collider other) 退出触发器的时候调用一次
范例代码
using UnityEngine;
public class CollisionClass : MonoBehaviour {
private void OnCollisionEnter(Collision other)
{
Debug.Log("碰撞开始了:"+other.collider);
//使碰撞物体变色
other.collider.GetComponent<MeshRenderer>().material.color = Color.green;
}
private void OnCollisionStay(Collision other)
{
Debug.Log("碰撞保持:" + other.collider);
}
private void OnCollisionExit(Collision other)
{
Debug.Log("碰撞结束了:" + other.collider);
//使碰撞物体变色
other.collider.GetComponent<MeshRenderer>().material.color = Color.red;
}
}
碰撞点的位置
可以选择点的形状及颜色
范例代码
using UnityEngine;
public class CollisionPosition : MonoBehaviour {
private void OnCollisionEnter(Collision other)
{
Debug.Log("碰撞点个数:"+other.contacts.Length);
for (int i = 0; i < other.contacts.Length; i++) {
Vector3 tempPos = other.contacts[i].point;
GameObject tempObj = new GameObject("Point");
tempObj.transform.position = tempPos;
}
}
}
mesh renderer即使隐藏亦可触发碰撞器
碰撞体和触发器二者只可选择其一,由isTrigger控制
摄像机图标隐藏
using UnityEngine;
public class cubeTrigger : MonoBehaviour {
//触发开始时调用一次
private void OnTriggerEnter(Collider other)
{
Debug.Log("触发开始"+other);
other.GetComponent<MeshRenderer>().material.color = Color.red;
}
//触发过程中每帧调用一次
private void OnTriggerStay(Collider other)
{
Debug.Log("触发中" + other);
}
//触发结束时调用一次
private void OnTriggerExit(Collider other)
{
Debug.Log("触发结束" + other);
other.GetComponent<MeshRenderer>().material.color = Color.blue;
}
}