- Home /
Weird Collision Detection with a Cube and OnCollisionEnter
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.
If you could post the code that would be most helpful :) What I'm thinking is your just confusing the collider with a rigidbody or navmeshagent, or the possibility your collider isn't a box collider.
It wouldn't let me comment so I edited the original post to include more info.
Answer by gouthammannuru · Aug 02, 2016 at 01:38 PM
Are these cubes Prefabs in that case delete the prefab and make a new prefab i had the same kind of problem and doing this (Deleting the prefab's) i got things to normal (I haven't checked your code )as i faced the problem previously and this is what i applied to get things right
Thanks for the help. I completely made the new cube prefab from scratch by creating new gameobjects and modifying their properties. Unfortunately, trying this does not seem to have made a difference.
Answer by rish209 · Aug 02, 2016 at 05:45 PM
While this isn't a real solution, as a temporary fix until I figure out the problem, I am setting this BoxCollider's size to new Vector3(0.8f, 0.8f, 0.8f) while the variable isCarried == true (one of the conditions in the OnCollisionEnter).