简体   繁体   中英

Unity 2D C# - no collision inside an object

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

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

在此处输入图片说明

you want to do something eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM