多米诺 发表于 2012-6-15 00:19

获取离自己最近的游戏对象的坐标

{:soso__9662691632502530860_2:}
//获取离自己最近的游戏对象的坐标.
function Update()
{
      if(networkView.isMine)
      {
            print(FindClosestEnemy().transform.position);
      }
      
}
// Find the name of the closest enemy
//查找最靠近敌人的信息
function FindClosestEnemy () : GameObject {
      // Find all game objects with tag Enemy
      //查找标签为test的全部游戏物体
      var gos : GameObject[];
      gos = GameObject.FindGameObjectsWithTag("test");
      var closest : GameObject=GameObject.FindWithTag("test");
      //var vect:Vector3 =new Vector3(1.0f,1.0f,1.0f);
      var distance = Mathf.Infinity;
      var position = transform.position;
      // Iterate through them and find the closest one
      //遍历他们找到最接近的一个
      for (var go : GameObject in gos) {
                if(go.transform.position==position)
                {
                        continue;
                }
                var diff = (go.transform.position - position);
                var curDistance = diff.sqrMagnitude;
                if (curDistance < distance) {
                        closest = go;
                        distance = curDistance;
                }
      }
      return closest;
}

gie32 发表于 2013-3-15 20:28


感谢楼主的无私分享!{:soso__11402694654016840197_7:}

arivd 发表于 2016-12-10 21:28


不错 不错 不错

liangxinyu 发表于 2017-3-11 08:32

很不错

seungsoo 发表于 2017-3-11 08:25

楼主是超人

tobia88 发表于 2017-3-11 08:07

好帖就是要顶

easygame 发表于 2017-3-11 08:09

真心顶

weisen 发表于 2017-3-11 08:50

很好哦

whseay 发表于 2017-3-21 10:30

顶顶多好

沧海一叶舟 发表于 2017-3-21 10:27

难得一见的好帖
页: [1]
查看完整版本: 获取离自己最近的游戏对象的坐标