Unity3D多物体碰撞关键代码
2016-09-21 17:08
260 查看
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<RectTransform>().position.x) * 0.05f; float ay = (ty – img1.GetComponent<RectTransform>().position.y) * 0.05f; img0.GetComponent<Player>().speedX -= ax; img0.GetComponent<Player>().speedY -= ay; img1.GetComponent<Player>().speedX += ax; img1.GetComponent<Player>().speedY += ay; } } } }
相关文章推荐
- unity3d和下OrtHello的使用教程 (二) 移动物体,碰撞等
- unity3d 简单碰撞检测代码
- Unity学习笔记 之 发射小球碰撞物体的代码记录
- 002-实现Unity3d中触摸屏幕控制物体的旋转和缩放 代码实现
- unity3d用鼠标拖动物体的一段代码
- unity3d用鼠标拖动物体的一段代码
- 关于Unity3d中物体之间碰撞检测的方法
- Unity3D粒子系统碰撞器,让粒子碰撞到物体之后消失
- 关于Unity3d中物体之间碰撞检测的方法
- Unity3d中物体的的碰撞检测
- Unity3D导入的FBX场景模型设置物体之间的碰撞
- unity3d用鼠标拖动物体的代码
- unity3d用鼠标拖动物体的一段代码 .
- unity3d相机跟随物体平滑移动(C#代码)
- UNITY3D 一行代码实现一个物体保持面向另一个物体(旋转)
- unity3d用鼠标拖动物体的一段代码
- unity3d用鼠标拖动物体的一段代码
- unity3d导入模型设置模型之间的物体碰撞
- unity3d 触屏拖拽物体代码
- unity3d用鼠标拖动物体的一段代码