描述

对于静态和动态几何形状,连续推断碰撞检测已开启

这是一种可以在动态和运动对象上使用的碰撞检测模式。该模式的成本通常低于其他 CCD 模式。此外,还可以更好地处理角运动。但是,在某些情况下,高速对象仍可能穿过其他几何形状。

//This script allows you to switch collision detection mode at the press of the space key, and move your GameObject. It also outputs collisions that occur to the console.
//Attach this script to a GameObject and make sure it has a Rigidbody component
//If it doesn't have a Rigidbody component, click the GameObject, go to its Inspector and click the Add Component button. Then, go to Physics>Rigidbody.
//Create another GameObject. Make sure it has a Collider, so you can test collisions between them.

using UnityEngine;

public class Example : MonoBehaviour { Rigidbody m_Rigidbody;

void Start() { //Attach this script to a GameObject. Ensure that it has a Rigidbody component. m_Rigidbody = GetComponent<Rigidbody>(); }

void Update() { //Press the space key to switch the collision detection mode if (Input.GetKeyDown(KeyCode.Space)) SwitchCollisionDetectionMode(); }

//Switch between the different Collision Detection Modes void SwitchCollisionDetectionMode() { switch (m_Rigidbody.collisionDetectionMode) { //If the current mode is continuous, switch it to continuous dynamic mode case CollisionDetectionMode.Continuous: m_Rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic; break; //If the current mode is continuous dynamic, switch it to continuous speculative case CollisionDetectionMode.ContinuousDynamic: m_Rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative; break;

// If the curren mode is continuous speculative, switch it to discrete mode case CollisionDetectionMode.ContinuousSpeculative: m_Rigidbody.collisionDetectionMode = CollisionDetectionMode.Discrete; break;

//If the current mode is discrete, switch it to continuous mode case CollisionDetectionMode.Discrete: m_Rigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous; break; } } }