- Home /
Collision Detection is too slow
I'm currently working on code to auto-generate levels/maps/prefabs/whatever. The auto-generation part works, and it works perfectly. The only problem I came across recently was when checking if the next space is empty or not. To do this I create a cube with the same size as the bounds of the original object. Add a rigidbody, freeze the position and add the script. The code below is the code I use for the cube (I call it testObject).
public GameObject previousPrefab;
public bool Collided;
void OnCollisionEnter(Collision col)
{
if (col.transform.root.gameObject != previousPrefab && col.transform.parent.name != "LevelKey")
{
Collided = true;
Debug.Log("Falsetestobject");
}
}
The following code is the code I use for placing this object and checking what value I get back from the "Collided" field.
public bool availableSpace(CustomGameObject go)
{
//Instantiates a new testobject
GameObject newprefab = go.getBoundsClone();
//Sets the previousprefab field to match the last prefab used in level generation
newprefab.GetComponent<testObject>().previousPrefab = lastUsedPrefab;
//adjusts the position and rotation of the testobject
newprefab.transform.eulerAngles = rotateGameObject(lastUsedPrefab, newprefab);
newprefab.transform.position = determinePosition(lastUsedPrefab, newprefab);
newprefab.transform.position -= childToParentDistance(newprefab, findBeginKey(newprefab));
Debug.Log(newprefab.GetComponent<testObject>().Collided.ToString());
if (newprefab.GetComponent<testObject>().Collided)
{
Debug.Log("false");
return false;
}
return true;
}
}
All this code does work perfectly except for one thing. The testObject should notify me when it's being placed over any object that isn't either it's own levelKey object or the previous placed object and return false in the availableSpace method. The problem is is that I first get the debug message "False" from the availableSpace method (at the bottom) and after that "Falsetestobject" from the testObject class a couple of times (once for every collision). I know this is because collision only happens after 1 frame has passed, so basically the placement is faster than the collision detection.
Is there ANY workaround or solution for this? Waiting after the placement of every testObject isn't really an option.
Still haven't found a way around this myself. Hoping someone who knows will see it this time around!
Quick question... is there anywhere that Collided becomes False again?
I too have this 'Escape' problem. Even tho I slow down rotations the object skips through the boundarary. I believe it could be a high torque value even though nothing is moving fast. I check this out tonight.
@meat5000, no. It doesn't become false again. If collision it true it means that the prefab was instantiated in another prefab. This means that there's no place and the game-object will be destroyed and replaced. Furthermore, the collision function is located on a temporary prefab that will always be destroyed even if it returns false. It's just a check.
@giantkiller I'm not moving or rotating anything in between frames. The problem I have is that before 1 frame has passed (to detect collision) there have already been a numerous number of new prefabs placed. In other words the code you see here finishes well before the first collision frame ever has a chance of taking place.
If you have a box which represents the bounds of the existing level you could check if the bounds of the new object intersect with it. That way you can be sure you check for intersection during the same frame that it's instantiated.
bool CheckIfIntersects(Bounds a, Bounds b){
if(a.Intersects (b)) return true;
return false;
}