- Home /
How to properly use Physics.Checkbox to ensure an object can be instantiated at a specific location without collision
Physics.Checkbox does not work at all as I would expect it. Therefore I would be glad for any help helping me to find where my usage of it is erronous. I have used GameObject.CreatePrimitive(PrimitiveType.Cube)
to create cubes at the very position Physics.CheckBox should test for collision and there is simply no object. Especially since I run these from UnityTest (PlaymodeTest) to ensure that the given object is placeable and that no other invisible collider exists in the scene..
var obj = /* GameObject with a BoxCollider 0.1,0.4,0.1 standing on- the floor. I have verified this */
var c = /* The BoxCollider of obj */
var instance = Object.Instantiate(obj);//Instantiates at 0,0,0 by default. The gameobject.transform is 0,0,0
instance.SetActive(false);//Set the gameobject inactive to disable collisions with instance
var position = Vector3.zero /* some position to test for collision. */
var p = position + c.transform.localPosition + c.center; // The position where the BoxCollider would be IF the object was placed at position.
Assert.IsFalse(Physics.CheckBox(p,//center of the would-be position of the collider
c.size / 2F,//equals c.extents
c.transform.rotation),"There should be no collision");//CHECK
instance.transform.position = Vector3.zero;
instance.SetActive(true);
//Instance now sits at 0,0,0 with a collider of 0.1, 0.4 , 0.1 such that 0,0,0 is the center bottom of the BoxCollider
Assert.IsTrue(Physics.CheckBox(p,//center of the would-be position of the collider
c.size / 2F,//equals c.extents
c.transform.rotation),"There should be collision");//ASSERT FAILED - there was no collision, but we expected collision
Maybe my math is simply wrong, but I am pretty sure it isn't as I have created cubes exactly at the position and size I use for CheckBox and they are exactly as I expect them to be. Maybe it has to do with testing collision during the same frame or that SetActive(false/true)
does not disable/enable the collisions for Physics.CheckBox.
I really do not see what could be wrong anymore, any help or guidance therefore is highly appreciated.
Thanks!