Version: 2023.1

MonoBehaviour.OnCollisionStay(Collision)

切换到手册

参数

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

描述

OnCollisionStay is called once per frame for every Collider or Rigidbody that touches another Collider or Rigidbody.

与 OnTriggerStay 相反,OnCollisionStay 被传入 Collision 类,而不是 ColliderCollision 类包含有关接触点、冲击速度等的信息。 如果在该函数中没有使用 collisionInfo,则不考虑 collisionInfo 参数,这样可避免不必要的计算。

Note: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached. 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.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void OnCollisionStay(Collision collisionInfo) { // Debug-draw all contact points and normals foreach (ContactPoint contact in collisionInfo.contacts) { Debug.DrawRay(contact.point, contact.normal, Color.white); } } }

OnCollisionStay 可以作为协同程序使用 - 在函数中使用 yield 语句即可。