I would like to check for a collision before an object is instantiated at the start of my game, for procedural level creation ‘road’ avoidance.
Currently I am using:
var oHit : RaycastHit; var objectPosition = new Vector3(Random.Range(-1.0,1.0) * length, 0, Random.Range(-1.0,1.0) * width); var checkResult = Physics.OverlapSphere(objectPosition, 10); if (checkResult.Length == 0) {place = Instantiate(object);
but it is not ‘accurate’ enough, and either avoids the objects entirely, or clips through them. the objects are complex and a simple sphere does not encompass them. I’d prefer a method that would use the mesh to determine collision.
I would like to use:
var checkResult = object.rigidbody.SweepTest(objectPosition, oHit, 10); if (checkResult == false) {place = Instantiate(object);
But it does not work in any way. it may be because I had to make the object a child in order to reset its transform values to zero for instantiation (like ‘freeze transformations’ in maya); thus it (the parent) has no collider?
for example:
objectParent(translate 0,0,0)
|-object (translate 3892,2873,972)