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.