- Home /
onCollisionEnter not working for ContinuousDynamic collision detection mode
Hi Guys, I am working on a game that needs to change the collision detection mode from Discrete to Continuous Dynamic and then once it collided, back to Discrete.
Well, it changes from Discrete to ContinuousDynamic but does not change frmo ContinuousDynamic to Discrete.
I am putting the code here using UnityEngine; using System.Collections;
public class freezePositionScript : MonoBehaviour { bool collidedWithFloor = false; bool changeMode = false; // Use this for initialization void Start () {
}
// Update is called once per frame
void FixedUpdate () {
if(laserCollision.placeObject && laserCollision._collidingObject.gameObject.name == this.gameObject.name)
{
print ("Placing object in FreezePosition");
this.rigidbody.collisionDetectionMode = CollisionDetectionMode.Continuous;
}
print ("collided with floor "+collidedWithFloor);
if(collidedWithFloor)
{
//this.rigidbody.collisionDetectionMode = CollisionDetectionMode.Discrete;
//this.rigidbody.constraints = RigidbodyConstraints.FreezePosition;
//this.rigidbody.collisionDetectionMode = CollisionDetectionMode.Discrete;
print ("Collision mode is "+this.rigidbody.collisionDetectionMode);
print ("Freezing position for "+this.gameObject.name);
changeMode = true;
collidedWithFloor = false;
}
if(changeMode)
{
this.rigidbody.collisionDetectionMode = CollisionDetectionMode.Discrete;
changeMode = false;
}
}
void OnCollisionEnter(Collision col)
{
collidedWithFloor = true;
print ("Collides in freeze position script");
}
void onCollisionStay(Collision col)
{
print ("Collsion for freezing the object");
if(laserCollision.placeObject && laserCollision._collidingObject.gameObject.name == this.gameObject.name && collidedWithFloor)
{
}
}
}
this script is applied to an object like Banana. After the collision detection mode is changed to Continuous Dynamic, the banana keeps on Jumping.
try onCollisionExit function .if there is no collisions this function is called so when there is no collision u can change the mode to Discrete hope this would helpfull
Thanks $$anonymous$$ohan but I have one constraint. When the object is falling, there is no collision so the collision with the collsion on object will not work. I have to use ContinuousDynamic mode because if the object falls on the ground, it must not go into space. With discrete collision mode the object goes below the ground and into blue space.
onCollision exit and discrete will not deactivate the collider of the gameobject .so when it is falling somewhere the collider is deactivating and passing through the wall so can u check on that.
Answer by ravi_gohil999 · Jan 13, 2014 at 06:40 AM
I have found an awesome script
http://wiki.unity3d.com/index.php?title=DontGoThroughThings
this things works well. Simple to implement. Make sure you assign the appropriate layer mask in the inspector when you use this script.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130234im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Find axis of collision 0 Answers
Collision Handling (Pushout and Sliding) for Customized Collision Object? 0 Answers
AI Avoidance Problem 1 Answer
Unity changes the collisionDetectionMode in builds 1 Answer
Problems with DragRigidBody.js 1 Answer