Box Collider(盒子碰撞器)
Capsule Collider(胶囊碰撞器)
Mesh Collider(网格碰撞器)
Sphere Collider(球碰撞器)
Terrain Collider(地形碰撞器)
Wheel Collider(车轮碰撞器)
碰撞器与触发器的区别
碰撞器是触发器的载体,而触发器只是碰撞器身上的一个属性。
当Is Trigger=false时,碰撞器根据物理引擎引发碰撞,产生碰撞的效果,可以调用OnCollisionEnter/Stay/Exit函数;
当Is Trigger=true时,碰撞器被物理引擎所忽略,没有碰撞效果,可以调用OnTriggerEnter/Stay/Exit函数.
如果既要检测到物体的接触又不想让碰撞检测影响物体移动或要检测一个物件是否经过空间中的某个区域,就可以用到触发器。
物体发生碰撞的必要条件
两个物体都必须带有碰撞器Collider,其中一个物体必须带有Rigidbody刚体。
检测
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestRigidbody : MonoBehaviour
{
void Start ()
{
}
void Update ()
{
}
void OnCollisionEnter(Collision collision)
{
}
void OnCollisionStay(Collision collision)
{
}
void OnCollisionExit(Collision collision)
{
}
void OnTriggerEnter(Collider other)
{
}
void OnTriggerStay(Collider other)
{
}
void OnTriggerExit(Collider other)
{
}
}
Collision 描述了一个碰撞。
public Vector3 relativeVelocity { get; }
摘要:The relative linear velocity of the two colliding objects (Read Only).两个碰撞物体的相对线速度(只读)。
public Rigidbody rigidbody { get; }
摘要:The Rigidbody we hit (Read Only). This is null if the object we hit is a collider with no rigidbody attached.
击中的刚体(只读)。如果命中的对象是一个没有刚体连接的碰撞器,则该值为null。
public Collider collider { get; }
摘要:The Collider we hit (Read Only).我们击中的物体的碰撞器(只读)。
public Transform transform { get; }
摘要:The Transform of the object we hit (Read Only). 我们碰撞到的对象的transform (只读)。
public GameObject gameObject { get; }
摘要:The GameObject whose collider you are colliding with. (Read Only). 碰撞到的GameObject(游戏物体)(只读)。
public ContactPoint[] contacts { get; }
摘要:The contact points generated by the physics engine. 物理引擎产生的接触点。
public Vector3 impulse { get; }
摘要:The total impulse applied to this contact pair to resolve the collision.应用在接触解决的碰撞的总的推动力。
ContactPoint 描述发生碰撞的接触点。
public Vector3 point { get; }
摘要:The point of contact.接触点。
public Vector3 normal { get; }
摘要:Normal of the contact point.接触点法线。
public Collider thisCollider { get; }
摘要:The first collider in contact at the point. 第一个接触的碰撞器。
public Collider otherCollider { get; }
摘要:The other collider in contact at the point.在那个点上的另一个碰撞器。
public float separation { get; }
摘要:The distance between the colliders at the contact point.碰撞器在接触点之间的距离。
Collider所有对撞机/碰撞器的基类。
public bool enabled { get; set; }
摘要:Enabled Colliders will collide with other Colliders, disabled Colliders won't.启用的碰撞器将与其他碰撞器碰撞,禁用的碰撞器不会。/是否开启碰撞器组件
public Rigidbody attachedRigidbody { get; }
摘要:The rigidbody the collider is attached to. 与对撞机相连的刚体。
public bool isTrigger { get; set; }
摘要:Is the collider a trigger? 是否开启触发器
public float contactOffset { get; set; }
摘要:Contact offset value of this collider. 这个对撞机的接触偏移值。
public PhysicMaterial material { get; set; }
摘要:The material used by the collider.对撞机使用的材质。
public PhysicMaterial sharedMaterial { get; set; }
摘要:The shared physic material of this collider.这台对撞机共有的物理材质。
public Bounds bounds { get; }
摘要:The world space bounding volume of the collider. 对撞机的世界空间边界体积。
public Vector3 ClosestPoint(Vector3 position);
摘要:Returns a point on the collider that is closest to a given location.返回对撞机上最接近给定位置的点。
public Vector3 ClosestPointOnBounds(Vector3 position);
摘要:The closest point to the bounding box of the attached collider. 与所附对撞机的边界框最近的点。
事件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
//鼠标进入
private void OnMouseEnter()
{
}
//鼠标在内
private void OnMouseOver()
{
}
//鼠标离开
private void OnMouseExit()
{
}
//鼠标按下
private void OnMouseDown()
{
}
//鼠标抬起
private void OnMouseUp()
{
}
//鼠标按下并抬起
private void OnMouseUpAsButton()
{
}
//鼠标拖拽
private void OnMouseDrag()
{
}
}