- Home /
Can't detect colliders from Tiled
I'm currently working on a 2D-bomberman-like game.
I've created a map in Tiled for it, where I put 2 layers: one for the Grass, one for the Walls(tiles used in Walls layer have colliders that were created in Tiled, too).
I want to detect what is in the area of bomb explosion with Physics2D.OverlapBox function. So there are 3 options:
1. There's a Wall
2. There's a Block(Prefab with a BoxCollider)
3. There isn't anything.
The function is detecting Blocks properly, but it has some problems with Walls. This is the code I've created for my tests and two pictures that show how it looks like.
There's also a function for drawing a Box, but I think it's not necessary to show it.
Anybody knows how to solve this? Tags are set properly, collisions with walls work properly ( I mean, I can't go through the wall). Sorry if something is messy, I'm new to all this.
void Update()
{
if (Input.GetKeyDown("e"))
{
ContactFilter2D filter = new ContactFilter2D();
Vector2 dimensionsOfBox = gameObject.GetComponent<SpriteRenderer>().bounds.size;
//I cut the dimensions in a half, so it doesn't detect the Blocks/Walls next to it.
Vector2 halfVector = Vector2.one / 2f;
dimensionsOfBox -= halfVector;
// box is above my character.
Vector2 positionOfBox = (Vector2)transform.position + Vector2.up;
DebugDrawBox(positionOfBox, dimensionsOfBox, 0f, Color.red, 5f);
Collider2D[] objs = new Collider2D[1];
bool foundBlockOrWalls = false;
Physics2D.OverlapBox(positionOfBox, dimensionsOfBox, 0f, filter, objs);
foreach(Collider2D collider in objs)
{if (collider)
{
foundBlockOrWalls = collider.tag == "Walls" || collider.tag == "Block";
if (foundBlockOrWalls)
{
if (collider.tag == "Block") Debug.Log("There's a block above me!");
if (collider.tag == "Walls") Debug.Log("There's a wall above me!");
}
}
}
}
}
Your answer
Follow this Question
Related Questions
How to set collision for an object with specific collider size? 3 Answers
How do I set an object's velocity to the velocity of an object that collided with it? 0 Answers
Collision detection problem 0 Answers
Make 2D projectile collide only from the outside 1 Answer
Collision Killing CPU/FPS/Performance - Suggestions?! 0 Answers