说明
下面的内容在代码基础语法和Unity基本的使用方法上不做过多介绍,仅对已入门并有兴趣的同学做为学习和拓展的资料
Unity的Collider可以检测射线、碰撞,但并没有提供检测点坐标包含的接口,这里记录一下如何实现BoxCollider检测点坐标包含的方法,并记录问题和优化方案。
这个方法主要是为了尽量避免Unity内置物理引擎的性能损耗,毕竟物理引擎是为了处理大量物理碰撞和触发器而存在的,在某些只需要判断位置区域的简单需求下,使用物理引擎去实现反而是一种很很浪费的方式。
相关API:
[NativeHeader("Modules/Physics/BoxCollider.h")]
[RequiredByNativeCode]
public class BoxCollider : Collider
{
public BoxCollider();
public Vector3 center {
get; set; }
public Vector3 size {
get; set; }
[Obsolete("Use BoxCollider.size instead.")]
public Vector3 extents {
get; set; }
}
[NativeHeader("Modules/Physics/Collider.h")]
[RequireComponent(typeof(Transform))]
[RequiredByNativeCode]
public class Collider : Component
{
public Collider();
public bool enabled {
get; set; }
public Rigidbody attachedRigidbody {
get; }
public bool isTrigger {
get; set; }
public float contactOffset {
get; set; }
public Bounds bounds {
get; }//注意!aabb盒
[NativeMethod("Material")]
public PhysicMaterial sharedMaterial {