help with collider2d center point

hi everyone, who im trying to do here is that i have the next comparation to check if my player is touching hanging objects:

        touchingHangingArea = Physics2D.OverlapCircle (hangingAreaCheck.position, hangingAreaCheckRadius, whatIsHangingArea);

and i have a box collider 2d attached to the hanging objects to make the player hanging of the object, the code works great but like i’m overlapping a circle it always does collision with the bottom boundary or the top boundary and what i want is to make the player always colliding with the center of the Y axis so doesn’t matter if he enters from top or down he always gonna be in the same point in the Y axis of the hangingObject collider here’s a picture if i can be more specific.

Is there a way to achieve this?? thanks you 2666301--188127--example.jpg

I’m not 100% certain what the behavior you’re looking for is, but if you want a specific Y value collider, try using an Edge Collider positioned to that Y coordinate and using that for collision checks.

Actualliy it can collide at any part of the red line but only the central point of the circle

To detect a point overlap, you can use Physics2D.OverlapPoint, or Collider2D.OverlapPoint. Perhaps using the circle as a trigger to start that check, you could do that check in an OnTriggerStay2D.

The edge collider should work for the red line collision.

Hi i’ve been testing out some ideas but i just can’t get it work correctly, this is because the circle always collide with the first line it touches and i don’t want that, what i want is the circle’s center point collide with the X center point (sorry if i said it wrong before) of the rectangle collider, for that i did what you told me and add a script that compares this trough a OnTriggerEnter2D but i jus can’t make its behavior work well. here’s and example of what i’m trying to do2668155--188241--example2.jpg

thanks for your help!!!

Well I will say that it will be very difficult to actually get that collision to happen, as that’s a very very precise collision you’re trying to check for. If the object moves too fast, it will skip over that position entirely. It is very unlikely that you will get those two points to be perfectly aligned.

One possible solution is to make the line a thin rectangle trigger (to make the area reasonably large to detect an overlapping point).

Inside that collider’s OnTriggerStay2D, you would check if the overlapping object is a circle object. If it is, you can do “if(collider.bounds.Contains(circle.position))” to check if the circle’s center is within the thin rectangle.

so i can check if the entire circle is inside of the rectangle?? that would work to but how?

I was suggesting that you check if the circle is overlapping the rectangle, and then once you know it is, additionally check if the circle’s centerpoint is within the rectangle. That way you’re only checking once the circle is close enough for it to matter.

hi thanks for your answer, i don’t know if it is too much to ask but could you write a little code example?? i don’t know the way bounds.Contains and those instructions work, thanks for the help

I imagine something like this:

// a script on the thin rectangle for the line
public class LineTrigger : MonoBehaviour {

    private Collider2D myCollider;

    private void Start() {
        myCollider = GetComponent<Collider2D>();
    }

    private void OnTriggerStay2D(Collider other) {
        // detect the circle somehow
        if(other.gameObject.tag == "circle") {
            // does the circle's center lie within this collider?
            if(myCollider.bounds.Contains(other.transform.position)){
                Debug.Log("circle center inside the line area!");
            }
        }
    }
}

There is actually no such thing as a point/line collision because the line is effectively infinity thin and the point is infinity small. The only way to consistently detect this is a line/line intersection i.e. performing a ray-cast against the edge collider.

Alternately, why not just use a really small CircleCollider2D and use continuous collision detection on the Rigidbody2D that is on each of these circles? It should be small enough to be visually insignificant.

But if you need to know exactly when it’ll intersect that edge then you could perform a Physics2D.Raycast using the circles velocity * fixed time-step as the distance. In other words, perform a raycast for the distance the circle moves each fixed update.

2 Likes

hi thanks for your answer, i already have the radius of the circleCollider very small, its size is 0.08 because if i make it smaller then the hanging objects don’t detect it but the animation sometimes don’t seem to look correct 'cause the circle is doing collision with the edges of the rectangle and the collision is never the same (because to player can enter from upside or downside) so the animation don’t look like the way is suppose to be, i’m trying to fix this in some way it always collide in a same point so the player and the hanging object look aligned

I think your best bet is to use the collider to detect a not-perfectly precise collision, and then set the object to the precise position you want.