Comments and answers for "detect side of collision in box collider 2d?"
http://answers.unity.com/questions/783377/detect-side-of-collision-in-box-collider-2d.html
The latest comments and answers for the question "detect side of collision in box collider 2d?"Comment by Kunal23082003 on Kunal23082003's answer
http://answers.unity.com/comments/1894178/view.html
I tried this script.But whenever the object collides from the top and bottom,The right collider gets hits.and when the object collides from left and right the script is working.When I removed the horizontal collision, Vertical collisions are working just fine.Anyone know why both of them are not working togetherSun, 27 Mar 2022 14:29:15 GMTKunal23082003Answer by Tioboon
http://answers.unity.com/answers/1863099/view.html
Here is a easier way to make that:
- Get the collision point by ClosestPoint
(https://docs.unity3d.com/2019.1/Documentation/ScriptReference/Collider2D.ClosestPoint.html)
- Get the distance from collision object center (using bounds.center) to closestPoint
- Get the angle of the distance
- Draw a box somewhere and trace lines to determine wich part of the box will make a different action.
- Create a enum for each one of them.
- Check the angles and return the enum.
Here is the code:
private static CollisionSide CheckIfFloorIsUnder(Collider2D t$$anonymous$$sCollider, Collider2D otherCollider)
{
var closestPoint = otherCollider.ClosestPoint(t$$anonymous$$sCollider.bounds.center);
var distance = closestPoint - (Vector2)otherCollider.bounds.center;
var angle = Vector2.Angle(Vector2.right, distance);
if (angle < 135 && angle > 45)
{
return CollisionSide.Under;
}
//The rest of sides by angle
return CollisionSide.None;
}
public enum CollisionSide
{
Under,
Above,
Sides,
None,
}Sun, 26 Sep 2021 15:36:08 GMTTioboonAnswer by brdavid_unity
http://answers.unity.com/answers/1692468/view.html
For the curious, I solved t$$anonymous$$s problem using the following code
private string WhatSideOfTheColliderWasHit(Collider2D collision)
{
Vector2 PointOnBoxHit = collision.ClosestPoint(transform.position);
Vector2 centerOfObject = collision.bounds.center;
float xMinPoint = Mathf.Abs((collision.bounds.size.x/2) - centerOfObject.x);
float xMaxPoint = Mathf.Abs(xMinPoint + collision.bounds.size.x);
float yMinPoint = Mathf.Abs((collision.bounds.size.y / 2) - centerOfObject.y);
float yMaxPoint = Mathf.Abs(yMinPoint + collision.bounds.size.y);
if (PointOnBoxHit.x >= xMinPoint && PointOnBoxHit.x <= xMaxPoint && Approximately(yMaxPoint, PointOnBoxHit.y, .1f))
return "Bottom";
else if (PointOnBoxHit.x >= xMinPoint && PointOnBoxHit.x <= xMaxPoint && Approximately(yMinPoint, PointOnBoxHit.y, .1f))
return "Top";
else if (PointOnBoxHit.y >= yMinPoint && PointOnBoxHit.y <= yMaxPoint && Approximately(xMaxPoint, PointOnBoxHit.y, .1f))
return "Right";
else
return "Left";
}
The basic idea is that I grab the closest point on the collider box where the collision happened. I then get the calculate the min/max of x and y of t$$anonymous$$s particular box. I then simply compare my point to these min/max values. It tells exactly what side of the box I am on.
Also Approximately is my own function that basically checks to see if the two values are wit$$anonymous$$n a threshold. It's like Mathf.Approximately but I can control the epsilon.
Edit: You may have a problem with certain blocks returning size = 0,0,0 in some cases. If t$$anonymous$$s happens then just go after size from the component
Vector3 size = collision.gameObject.GetComponent<BoxCollider2D>().size;
That fixed the issue for me. Then for the code above that calls for size, just replace collision.bounds.size with just size.Sun, 19 Jan 2020 21:47:27 GMTbrdavid_unityComment by brdavid_unity on brdavid_unity's answer
http://answers.unity.com/comments/1692422/view.html
From researching and programming this, this will not work. The code as it stands cannot determine if the contact is truly on the bottom when it could actually be on the lower left or lower right and still be true for all checks for the bottom condition.
One Idea that I have is to find the angle from the center to the point of contact. If that angle appears in a specific part of the box, we have our side.
For example from the center to the topleft and topright there is an angle between the two lines which connect to these corners from the center. If my contact point is between these two angles then I know exactly what side I am on. I can assume a side if the angle is equal.Sun, 19 Jan 2020 17:59:44 GMTbrdavid_unityComment by Vasile-Peste
http://answers.unity.com/comments/1653763/view.html
Take a look at this https://www.malgol.com/how-to-detect-collision-side-in-unity-2d/ should work with any collider. Just download and import the code.Thu, 01 Aug 2019 16:09:11 GMTVasile-PesteComment by alexjhones286 on alexjhones286's answer
http://answers.unity.com/comments/1457526/view.html
Obrigado amigo'. Brasil aqui'. :)Sat, 20 Jan 2018 18:42:51 GMTalexjhones286Comment by Kodekool on Kodekool's comment
http://answers.unity.com/comments/1160304/view.html
Thanks. I'd actually done a lot of reading on this since posting the question so I did understand, but I really appreciate you taking the time to give me this explanation.Thu, 24 Mar 2016 17:13:32 GMTKodekoolComment by ValderZhu on ValderZhu's answer
http://answers.unity.com/comments/1160022/view.html
![Collision][1]
Refer to the image. The rectangle is your collider and the circle is the incoming object.
If ( contactPoint.x > center.x ), this means that the center of circle is on the right of center of rectangle.
If (contactPoint.y > center.y ), the center of circle is on the top of center of rectangle.
You may want to check the coordinates with stronger conditions, like:
void OnCollisionEnter2D(Collision2D collision)
{
Collider2D collider = collision.collider;
bool collideFromLeft;
bool collideFromTop;
bool collideFromRight;
bool collideFromBottom;
int RectWidth = this.GetComponent<Collider2D> ().bounds.size.x;
int RectHeight = this.GetComponent<Collider2D> ().bounds.size.y;
int circleRad = collider.bounds.size.x;
if(collider.name == "target")
{
Vector3 contactPoint = collision.contacts[0].point;
Vector3 center = collider.bounds.center;
if (contactPoint.y > center.y && //checks that circle is on top of rectangle
(contactPoint.x < center.x + RectWidth / 2 && contactPoint.x > center.x - RectWidth / 2)) {
collideFromTop = true;
}
else if (contactPoint.y < center.y &&
(contactPoint.x < center.x + RectWidth / 2 && contactPoint.x > center.x - RectWidth / 2)) {
collideFromBottom = true;
}
else if (contactPoint.x > center.x &&
(contactPoint.y < center.y + RectHeight / 2 && contactPoint.y > center.y - RectHeight / 2)) {
collideFromRight = true;
}
else if (contactPoint.x < center.x &&
(contactPoint.y < center.y + RectHeight / 2 && contactPoint.y > center.y - RectHeight / 2)) {
collideFromLeft = true;
}
}
}
[1]: /storage/temp/66561-collision.pngThu, 24 Mar 2016 03:04:23 GMTValderZhuAnswer by Kodekool
http://answers.unity.com/answers/1074596/view.html
T$$anonymous$$s is going to be a little basic of me, but please forgive my ignorance, can anyone explain a bit as to why t$$anonymous$$s works? or point me to a resource where I can read more about it. i'm a little confused.Thu, 01 Oct 2015 12:02:37 GMTKodekoolComment by nanthsree22 on nanthsree22's answer
http://answers.unity.com/comments/935818/view.html
How would I do this for an OnTriggerEnter2D function?Mon, 30 Mar 2015 05:29:09 GMTnanthsree22Comment by thusitha on thusitha's answer
http://answers.unity.com/comments/785441/view.html
Thank you for the effort @kacyesp. I used ray Physics2D.Linecast to draw a line and detect the side. thank you again for the answer.Fri, 05 Sep 2014 11:01:13 GMTthusithaAnswer by kacyesp
http://answers.unity.com/answers/783413/view.html
Use collider.bounds.center and compare it with the point.
void OnCollisionEnter2D(Collision2D collision)
{
Collider2D collider = collision.collider;
if(collider.name == "target")
{
Vector3 contactPoint = collision.contacts[0].point;
Vector3 center = collider.bounds.center;
bool right = contactPoint.x > center.x;
bool top = contactPoint.y > center.y;
}
}Tue, 02 Sep 2014 11:26:43 GMTkacyesp