- Home /
I can't manage to detect my collisions
You see, I'm creating a random dungeon generator. The rooms are all tile mapped, and I need to check if they are colliding with any other rooms when they get spawned in (I'm using the "create an array of rooms, then randomly select one to instantiate" design.)
Unfortunately, Collider2D.OverlapCollider and Collider2D.IsTouchingLayers (I ditched this one) don't seem to register. Also, I tried switching to CompositeColliders, but it didn't do any better. I also tried using OnCollisionEnter/Stay2D for this, but that didn't work either.
Here's my code for all it's worth:
public class RoomCollisionDetector : MonoBehaviour {
public SpawnPoint SpawnedFrom;
private float time;
void Update() {
Debug.Log ("I AM DOING THIS OKAY??!?!??!?!??!?!??!??");
time += Time.deltaTime;
if (gameObject.GetComponent<Collider2D> ().OverlapCollider (new ContactFilter2D (), new Collider2D[15]) != 0) {
Debug.Log ("Destroying a room!");
SpawnedFrom.StopAllCoroutines ();
SpawnedFrom.StartCoroutine (SpawnedFrom.SpawnRoom ());
Destroy (SpawnedFrom.Room);
Rooms.TotalRooms--;
}
if (time >= 1.5) {
Destroy (this);
}
}
}
There is also a lot of other scripts, but I doubt that any of them affect the collision detection in any way. Oh, except that right before it enables this script, it waits for a fixed update, and that didn't help either (I even tried TWO wait for fixed updates, but it still wouldn't work)
The stopallcoroutines part is just to prevent any more work involving this room before it is destroyed. It is quite possible that that part breaks my entire thing, but I just want to focus on the collision detection for now. In other words, The only issue anyone I want to focus on is the collision detection.
If anyone may be able to help me fix this, it would be highly appreciated. Thank you in advance.
Your answer
Follow this Question
Related Questions
Layers of collision, or multiple collisions 1 Answer
Make 2D projectile collide only from the outside 1 Answer
OnCollisionEnter2D is not working when an object tagged "Enemy" is not present. 2 Answers
Bullet hit the collider of a child object 1 Answer
Physics.OverlapCircle not working when Layermask is applied 0 Answers