找回密码
 立即注册
Unity开发者联盟 门户 查看主题

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

发布者: 多米诺 | 发布时间: 2012-6-15 00:19| 查看数: 5036| 评论数: 2|帖子模式

{:soso__9662691632502530860_2:}

  1. //获取离自己最近的游戏对象的坐标.
  2. function Update()
  3. {
  4.         if(networkView.isMine)
  5.         {
  6.             print(FindClosestEnemy().transform.position);
  7.         }
  8.         
  9. }
复制代码
  1. // Find the name of the closest enemy
  2. //查找最靠近敌人的信息
  3. function FindClosestEnemy () : GameObject {
  4.         // Find all game objects with tag Enemy
  5.         //查找标签为test的全部游戏物体
  6.         var gos : GameObject[];
  7.         gos = GameObject.FindGameObjectsWithTag("test");
  8.         var closest : GameObject=GameObject.FindWithTag("test");
  9.         //var vect:Vector3 =new Vector3(1.0f,1.0f,1.0f);
  10.         var distance = Mathf.Infinity;
  11.         var position = transform.position;
  12.         // Iterate through them and find the closest one
  13.         //遍历他们找到最接近的一个
  14.         for (var go : GameObject in gos) {
  15.                 if(go.transform.position==position)
  16.                 {
  17.                         continue;
  18.                 }
  19.                 var diff = (go.transform.position - position);
  20.                 var curDistance = diff.sqrMagnitude;
  21.                 if (curDistance < distance) {
  22.                         closest = go;
  23.                         distance = curDistance;
  24.                 }
  25.         }
  26.         return closest;
  27. }
复制代码

最新评论

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
难得一见的好帖

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-6-15 10:20 , Processed in 0.090333 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表