Unity 2D C# - no collision inside an object

288 views Asked by At

I want to know, how to get no collision inside an object. I have a big circle and inside the circle is a smaller square. When the game starts, the circle is scaling down. And what i want, is to check the collision, if circle is touching or is inside the square.

Can you please help me ? Thank you

1

There are 1 answers

6
Keiwan On

As far as I know there is no built-in way of achieving this in Unity (There is also not a built-in way of detecting whether a collider is fully inside another collider or not).

If I understood you correctly your circle is going to shrink and when it reaches this point

enter image description here

you want to do something e.g. execute some code.

The way you could make it work for a circle and a square just based on maths would be this:

If the sides of your square are of length a and your circle has a decreasing radius of r, then at the moment you see in the image above the relation between the two of them is:

r = a / sqrt(2)

So you could check if (r <= a / Mathf.Sqrt(2)) in the Update function and based on that call some function. (Maybe add another boolean to ensure the function only gets called once.)

You can get your sprite widths using

width = GetComponent<SpriteRenderer>().bounds.size.x;

The radius of your circle would then obviously be half the width of the circle sprite.

You should probably also store the SpriteRenderer in a variable once instead of calling GetComponent on every frame.

For an equilateral triangle the equation would be

r = a / sqrt(3)

where a is the length of the triangle's side.