void Update () {
for(int i=0; i<images.Length-1; i++)
{
Image img0 = images[i];
for(int j=i+1; j<images.Length; j++)
{
Image img1 = images[j];
float dx = img1.GetComponent<RectTransform>().position.x – img0.GetComponent<RectTransform>().position.x;
float dy = img1.GetComponent<RectTransform>().position.y – img0.GetComponent<RectTransform>().position.y;
float dist = Mathf.Sqrt(dx * dx + dy * dy);
float minDist = img0.GetComponent<RectTransform>().rect.width / 2 + img1.GetComponent<RectTransform>().rect.width/2;
if (dist < minDist)
{
float angle = Mathf.Atan2(dy, dx);
float tx = img0.GetComponent<RectTransform>().position.x + dx / dist * minDist;
float ty = img0.GetComponent<RectTransform>().position.y + dy / dist * minDist;
float ax = (tx – img1.GetComponent<RectTra
Unity3D多物体碰撞关键代码
最新推荐文章于 2024-08-21 09:33:10 发布
该博客详细介绍了在Unity3D中处理多物体碰撞的关键代码,通过遍历图片数组,计算每对图片之间的距离,并判断是否发生碰撞。当碰撞发生时,根据角度和最小距离调整物体速度,实现碰撞响应。
摘要由CSDN通过智能技术生成