- Home /
How to detect which side of a box collider was hit? (Top, Bottom, Right, Left) 2D game/ C#
I've been thinking about this for the past two days. Been searching on the net but can't find anything.
I came up with a code using empty game objects to show the sides of the box, but I'm sure there's a more optimal and good way to do this. It's pretty basic stuff but can't find anything on the subject. I've tried collision contact points, but there was a bug where if the top part had a collision, the LEFT or RIGHT side was also a collision.
Is there an efficient/clean way to discover which sides of a box collider had a collision, or am I better using raycasts? In my game, the player is using raycasts, having no problem detecting the side of the player which has a collision. But with a box collider it's a bit more complex it seems.
Thank you for anyone considering helping me. :)
Answer by toddisarockstar · Feb 22, 2018 at 09:33 PM
why not simply compare the positions of the two objects?
void OnCollisionEnter(Collision c)
{ // get the direction of the collision
Vector3 direction = transform.position - c.gameObject.transform.position;
// see if the obect is futher left/right or up down
if (Mathf.Abs (direction.x) > Mathf.Abs (direction.y)) {
if(direction.x>0){print("collision is to the right");}
else{print("collision is to the left");}
}else{
if(direction.y>0){print("collision is up");}
else{print("collision is down");}
}
}
Because it may collide with a Tilemap collider, where the position of the entire tilemap is not relevant for the collision direction
This is generally a poor method. Consider a cube object for the floor: if you fall on it, the collision is direction below you, on your feet, neither left nor right. But the center of the floor could be anywhere. Or you might run leftwards into an L-shaped object, where the center pivot of the object is actually to your right.
Basically the pivot's transform.position can't generally be assumed to be a good indication of where a collision is. Unless the object is simple and convex like a cube or sphere.
Answer by regnared · Jan 17, 2013 at 02:40 AM
If anyone finds this question, I was able to use raycasting for my problem. I posted a script and video here if you are curious.
Youtube Link - [link ]
Script Link - [link ]
You are kidding, right? He posted a link to a youtube video in the answer above. In the video description he posted the link to his script
This is a bad way to do it. You should ins$$anonymous$$d place one box collider on each side to do the detection. It does involve a lot of box collider, but it will be incredibly faster than using raycasts for no valid reason.
Yep this is old but hey. $$anonymous$$y brain tells me that finding local contact point would be simple; its in the hit info. From origin of object its just a simple case of finding the direction to that point and deciding which face its on based on the axes.
Answer by jtagrawal2000 · Oct 09, 2020 at 07:15 AM
Use this to look for direction of collision If this is included in script of a object which collides with c then this will print which side of c did a hit , you can also look to see if it hit the corners by checking the direction’s values
void OnCollisionEnter(Collision c)
{
Vector2 direction = c.GetContact(0).normal;
If( direction.x == 1 ) print(“right”);
If( direction.x == -1 ) print(“left”);
If( direction.y == 1 ) print(“up”);
If( direction.y == -1 ) print(“down”);
}