The collision detection mode constants used for Rigidbody.collisionDetectionMode.
//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; public float m_Speed;
void Start() { //Fetch the Rigidbody of the GameObject (make sure this is attached in the Inspector window) m_Rigidbody = GetComponent<Rigidbody>(); //Make sure the Rigidbody can't rotate or move in the z axis for this example m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ; }
void Update() { //Change the GameObject's movement in the X axis float translationX = Input.GetAxis("Horizontal") * m_Speed; //Change the GameObject's movement in the Y axis float translationY = Input.GetAxis("Vertical") * m_Speed;
//Move the GameObject transform.Translate(new Vector3(translationX, translationY, 0));
//Press the space key to switch the collision detection mode if (Input.GetKeyDown(KeyCode.Space)) SwitchCollisionDetectionMode(); }
//Detect when there is a collision starting void OnCollisionEnter(Collision collision) { //Ouput the Collision to the console Debug.Log("Collision : " + collision.gameObject.name); }
//Detect when there is are ongoing Collisions void OnCollisionStay(Collision collision) { //Output the Collision to the console Debug.Log("Stay : " + collision.gameObject.name); }
//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; } } }
Discrete | Continuous collision detection is off for this Rigidbody. |
Continuous | Continuous collision detection is on for colliding with static mesh geometry. |
ContinuousDynamic | Continuous collision detection is on for colliding with static and dynamic geometry. |
ContinuousSpeculative | Speculative continuous collision detection is on for static and dynamic geometries |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.