In my project, I use OnCollisionEnter to detect when a cube hits another cube. For some reason, the cubes detect each other without actually colliding. My program has the cubes fall when they collide and each time they start to fall, they are a small distance away from each other.
Careful testing reveals that the area in which the collisions are detected are in what seems to be a sphere with diameter sqrt 3 (cubes have side length 1). Is there any way to fix this or figure out why it is happening?
Edit (Additional Info and Code):
I checked and my cube definitely has a box collider and no other colliders. It is also not the child of an object. It has spheres with no rigidbodies and colliders set to triggers as children but deleting them had no visual effect on the problem.
Here is the OnCollisionEnter which sets a variable that is used in another script.
Note: There is some code related to SteamVR since I am building this game as a virtual reality experience.
void OnCollisionEnter(Collision other)
{
if (other.collider.gameObject.CompareTag("Connecter") == false)
{
if (other.collider.gameObject.name != "Ground")
{
if (other.collider.gameObject.GetComponent<Held>().isCarried() == true && this.isCarried() == true)
{
//yetAnotherBool = true;
otherP = other;
var deviceL = SteamVR_Controller.Input(indexL);
var deviceR = SteamVR_Controller.Input(indexR);
var rb = this.gameObject.GetComponent<Rigidbody>();
var otherRB = other.collider.gameObject.GetComponent<Rigidbody>();
if (rb.velocity.magnitude > otherRB.velocity.magnitude)
{
if (Mathf.Abs(rb.velocity.magnitude - deviceL.velocity.magnitude) > Mathf.Abs(rb.velocity.magnitude - deviceR.velocity.magnitude))
{
MotherScript2.booleanR = true;
}
else
{
MotherScript2.booleanL = true;
}
}
}
}
}
}
Here is a place where the function ReleaseCube() is called:
else if (PickUpJoint != null || boolean == true)
{
if (BlockP.gameObject.GetComponent<Held>().isHeld() == true)
{
if (trackedController.triggerPressed == false || boolean == true)
{
ReleaseCube(controller);
}
}
} //Remove Fixed Joint and Release Cube
Here is the ReleaseCube function:
public void ReleaseCube(Transform controller)
{
index = (int)trackedController.controllerIndex;
var device = SteamVR_Controller.Input(index);
Debug.Log("Test");
GameObject go = PickUpJoint.gameObject;
Rigidbody rigidbody = go.GetComponent<Rigidbody>();
Object.Destroy(PickUpJoint);
PickUpJoint = null;
SetIsHeld(false, "OnTriggerStay in " + controller.gameObject.name);
BlockP.gameObject.GetComponent<Held>().isCarried(false);
this.SetIsHolding(false, "OnTriggerStay in " + this.gameObject.name);
BlockCounter--;
var origin = trackedObj.origin ? trackedObj.origin : trackedObj.transform.parent;
if (origin != null)
{
rigidbody.velocity = origin.TransformVector(device.velocity);
rigidbody.angularVelocity = origin.TransformVector(device.angularVelocity);
}
else
{
rigidbody.velocity = device.velocity;
rigidbody.angularVelocity = device.angularVelocity;
}
rigidbody.maxAngularVelocity = rigidbody.angularVelocity.magnitude;
}
Sorry for the confusing code. Here is what it does/is supposed to do:
The first part detects collisions between two carried cubes and finds which one is moving faster and what controller it is held by. It then sets a boolean true (the boolean is set false constantly in an update loop).
The second part checks to see if the cube is being held and the trigger isn’t pressed or if the boolean is true. It then calls the ReleaseCube function.
The third part destroys the joint between the controller and the cube, sets a bunch of variables that say the cube is held to false, and sets the cube’s velocity to equal that of the controller.