- Home /
How to detect if a specific side of a collider is touching another collider
I am making a mario style platformer and want to execute some code when the head of my sprite is touching another box collider 2d
Answer by Capricornum · Sep 14, 2020 at 07:51 AM
Why don't you just create a little box collider 2D and put it at the top of your Characters head? A little tiny brain collider or haircut collider. Just covering the part which can activate boxes. You can even put it on an otherwise empty game object as a child to your character. Now put another script on there that checks for collision with boxes. (GameObjects tagged as Box) Then you can call the boxes IGotKicked
method releasing the boxes reward.
@Capricornum how would I make it so the script detecting the collision is only detecting a collision for the top boxcollider2d
That's a good question. $$anonymous$$y first idea was that you have your $$anonymous$$ario with its own collider.
As a child of $$anonymous$$ario you create a new object called $$anonymous$$y Head Collider. It is basically an empty object with a box collider and a little script. You position this empty object at the top of the head, size the box collider appropriately and add a script that says.
OnCollisionEnter(Collision collision)
{
HeadbuttableBox box = collision.collider.GetComponent<HeadbuttableBox>()
if (box != null)
{
box.IGotHeadbutted();
}
}
@Capricornum how would I change what happens when the IGotHeadbutted method got called, and I can't find a component called HeadbuttableBox in the list
Answer by OneTrickPonny · Sep 14, 2020 at 06:15 AM
If you want your player to be able to jump trough platforms the best way to do that is using platform effectors. Here is a tutorial about how to make a 2d platformer character controller:
Answer by KreyZgr · Sep 14, 2020 at 01:01 PM
My idea would be that you can put a second 2d collider on top of the head of your Player. You need to set it to isTrigger and make it execute some code when other specific colliders enter it.
Your answer
Follow this Question
Related Questions
The player randomly freezes in place while other objects move ingame 1 Answer
Adding knockback to a 2D game (in C#) 2 Answers
2D Platformer Pick up and move object script help! 1 Answer
Problem with Falling Platform Script 3 Answers
How can I stop hurt animation and bounce the player when hit by an enemy? 0 Answers