Version: 2022.3

MonoBehaviour.OnCollisionStay2D(Collision2D)

切换到手册

参数

other 与该碰撞关联的 Collision2D 数据。

描述

在另一个对象上的碰撞体正在接触该对象的碰撞体时发送每个帧(仅限 2D 物理)。

在调用期间传入的 Collision2D 参数中报告有关所涉及的对象的进一步信息。如果不需要该信息,则可以声明没有参数的 OnCollisionStay2D。

Note: Collision events will be sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions. Collision stay events are not sent for sleeping Rigidbodies.

另请参阅:Collision2D 类、OnCollisionEnter2DOnCollisionExit2D

using UnityEngine;

public class Example : MonoBehaviour { float rechargeRate = 10.0f; float batteryLevel;

void OnCollisionStay2D(Collision2D collision) { if (collision.gameObject.tag == "RechargePoint") { batteryLevel = Mathf.Min(batteryLevel + rechargeRate * Time.deltaTime, 100.0f); } } }