[英]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). 据我所知,在Unity中没有实现此目的的内置方法(也没有检测对撞机是否完全位于另一对撞机内部的内置方法)。
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: 如果正方形的边长为
a
而圆的半径为r
递减,那么此时您在图像上方看到的这两者之间的关系是:
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. 因此,您可以检查
Update
函数中是否基于该函数调用if (r <= a / Mathf.Sqrt(2))
。 (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. 您可能还应该将
SpriteRenderer
存储在一个变量中,而不是在每个帧上都调用GetComponent
。
For an equilateral triangle the equation would be 对于等边三角形,等式为
r = a / sqrt(3)
where a
is the length of the triangle's side. 其中
a
是三角形边的长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.