触发器与发生触发检测的条件
trigger即为触发器
发生触发检测的必要条件:
①双方挂载Box Collider组件
②两者之一为触发器
③一方有刚体且最好是运动的一方
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HealthCollectible : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
RubyController rubyController = collision.GetComponent<RubyController>();
rubyController.ChangeHealth(1);//加血
Destroy(gameObject);//摧毁游戏对象
}
}