- Home /
How to the detect what side of the player is colliding with a object in a tilemap
i'm making a 2d vertical scrolling game, all the world's objects (walls, obstacles, etc.) are part of tilemaps, when the player collides with any object i need to get what side of the player collided, i've tried all the answers in this post and found similar solutions on this forum, the problem is, all of them use the position of the colliding object to find the side, that worked when colliding with standalone objects but didnt work when colliding with tiles in a tilemap since the collider always return the same fixed position of the Tilemap object. I know there mey be another ways to solve this issue but i just dont know what to do to get this working properly.
I've not worked with tilemap objects, and I'm just thinking off the top of my head, so with that caveat, have you considered that since you have the tilemap and the object known at the time of the collision, perhaps you could check the world position of the colliding object against the position of the tilemap to calculate the relative position of the object at the moment of the collision?
Answer by theGreatSlutze · Jul 12, 2018 at 12:01 PM
Can you use the position of the collision relative to the colliding player to figure out what side was collided with? When you receive a collision you should know the location of the collision. E.g. if it's greater than the X location of your player's center, but less than the extents of the player's bounding box on the Y axis, then you'd know the collision occurred on the right side of the player.
I think i've tried something like that before, but maybe not, could you please give me an example of what the code for this would look like?
Answer by sisse008 · Jul 12, 2018 at 12:07 PM
there are only two sides right? left and right? why not put on the player a "right" collider and a "left" collider?
There is top and bottom collisions too, i put 4 trigger colliders around the player box collider, but the reason i'm asking here is because it doesnt work very well, sometimes it detects a top collision when its hitting a wall on the left and so on, that's how it looks like right now:
the front collider got this way when we tried to fix the issue i said above, but it generates other problems when like when a wall hits the uncovered regions of the trigger colliders, besides that i think it would be simpler do deal with all this without creating extra colliders, but im open to any solution that actually works.
Your answer
Follow this Question
Related Questions
Composite Collider on Tilemap not really stopping player 0 Answers
How do I get collisions between Tilemap Collider 2d and a Kinematic Rigidbody 2d? 1 Answer
Weird ContactPoint2D on two BoxCollider2D collision 0 Answers
How do I set an object's velocity to the velocity of an object that collided with it? 0 Answers
How to detect on which exact tile a collision happened? 2 Answers