//都有碰撞器组件
//运动的物体有刚体组件
private void OnCollisionEnter(Collision other )
{
print(other.collider.name);
}
//都有碰撞器组件
//其中一个勾选IsTriggger
//其中一个有刚体组件
private void OnTriggerEnter(Collider other)//触发开始执行 other碰撞到的物体
{
float a = other.GetComponent<Enemy>().Hp;
print(other.name+a);
other.GetComponent<Enemy>().Hp -= Random .Range (5,10);
print(other.name + other.GetComponent<Enemy>().Hp);
}
private void OnTriggerStay(Collider other)//触发后每帧执行
{
other.GetComponent<MeshRenderer>().material.color = Color.red;
if (other.GetComponent<Enemy>().Hp <= 0)
Destroy(other.gameObject);
}
private void OnTriggerExit(Collider other)//触发结束时执行
{
other.GetComponent<MeshRenderer>().material.color = Color.white;
}
碰撞,触发
2020-04-13