Unity3D-刚体与触发器组件

部分内容由千峰教育(莫新宇)听课笔记总结

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;
    }
}


 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值