- Home /
How to detect collision side in 2d tile blocks game?
Hello I have problem detecting collision side on my blocks. I have world made from 2d blocks and I have player which I can controll ! I want to be able to destroy blocks when there is collission with player + BUTTON E is pressed ! Right now if my player is colliding with 2 blocks at the same time and if I press E BUTTON bouth blocks are destroyed ! So what I want is to destroy only one block at the time ! For example if player is moving right and it has obstacle block I want to destry it when E is pressed but at the same time if there is one more block under player then keep that botom block and destroy only one which is on right side - how to make this happen ? How to detect collision side on my blocks like collision was from left side or right or up or down? Please help
Answer by rv0000s · Mar 09, 2015 at 08:59 AM
you can use raycast to detect blocks on your sides when you hold right button and press E and then destroy it.
something like this:
RaycastHit2D hit = Physics2D.Raycast(player_position, Vector2.right, boxlayer);
if(hit.collider.gameobject==box){
Destroy(hit.collider.gameobject);
}
Your answer
Follow this Question
Related Questions
destroying gameobject from other script 1 Answer
Pacman eating (destroying) pellets on collision! 0 Answers
Destroy object on touch of object with specific class 3 Answers
Collision not working for 2d platformer 2 Answers
Simple on Collision Help (C#) 2 Answers