Object being pushed out from colliders inner space

Hello
I am trying to detect collision of one game object with another from inside: 2nd object is spawned inside 1st objects collider. I want it to start falling and stop when it reaches 1st objects collider edge.
Instead, after 2nd object was spawned it is being pushed out of 1st objects collider right away.
Here is cube with boxcollider2d and rigidbody2d inside empty gameobjects circlecollider2d


Here is what happens when i run scene

Can this behaviour be changed in a way i’ve described above?
Thanks

Colliders are not just edges and empty on the inside, they represent a physical 2D area.

If you want to make a hollow circle, your best bet is to use an EdgeCollider2D.

I found this script in a forum post awhile back and updated it to work a little more nicely:

using UnityEngine;

[ExecuteInEditMode, RequireComponent(typeof(EdgeCollider2D))]
public class CircleEdgeCollider2D : MonoBehaviour
{
    private const int MIN_POINTS_FOR_SHAPE = 3;
    private const float MIN_RADIUS_FOR_COLLIDER = 0.005f;

    [SerializeField] private float radius = 1f;
    [SerializeField] private int pointCount = 36;

    private EdgeCollider2D edgeCollider;

    /// <summary>
    /// The radius of the circle.
    /// </summary>
    public float Radius
    {
        get { return radius; }
        set
        {
            radius = Mathf.Max(MIN_RADIUS_FOR_COLLIDER, value);
            OnPropertyChanged();
        }
    }

    /// <summary>
    /// Number of points in the circle.
    /// </summary>
    public int PointCount
    {
        get { return this.pointCount; }
        set
        {
            pointCount = Mathf.Max(MIN_POINTS_FOR_SHAPE, value);
            OnPropertyChanged();
        }
    }

    /// <summary>
    /// The EdgeCollider2D being affected.
    /// </summary>
    public EdgeCollider2D EdgeCollider {
        get
        {
            if(edgeCollider == null)
            {
                edgeCollider = GetComponent<EdgeCollider2D>();
            }
            return edgeCollider;
        }
    }

    /// <summary>
    /// Start this instance.
    /// </summary>
    private void Start()
    {
        CreateCircle();
    }

    /// <summary>
    /// Redraws the circle when a property is changed
    /// </summary>
    private void OnPropertyChanged()
    {
        CreateCircle();
    }

    /// <summary>
    /// Creates the circle.
    /// </summary>
    private void CreateCircle()
    {
        Vector2[] edgePoints = new Vector2[PointCount + 1];

        for(int i = 0; i <= PointCount; i++) {
            float angle = (Mathf.PI * 2.0f / PointCount) * i;
            edgePoints[i] = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle)) * Radius;
        }

        EdgeCollider.points = edgePoints;
       
    }

    /// <summary>
    /// Validates input.
    /// </summary>
    private void OnValidate()
    {
        PointCount = pointCount;
        Radius = radius;
    }
}
1 Like