找回密码
 立即注册
查看: 4946|回复: 102

[脚本] 获取离自己最近的游戏对象的坐标

[复制链接]
发表于 2012-6-15 00:19 | 显示全部楼层 |阅读模式
{: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. }
复制代码
发表于 2013-3-15 20:28 | 显示全部楼层

感谢楼主的无私分享!{:soso__11402694654016840197_7:}
发表于 2016-12-10 21:28 | 显示全部楼层

不错 不错 不错
发表于 2017-3-11 08:32 | 显示全部楼层
很不错
发表于 2017-3-11 08:25 | 显示全部楼层
楼主是超人
发表于 2017-3-11 08:07 | 显示全部楼层
好帖就是要顶
发表于 2017-3-11 08:09 | 显示全部楼层
真心顶
发表于 2017-3-11 08:50 | 显示全部楼层
很好哦
发表于 2017-3-21 10:30 | 显示全部楼层
顶顶多好
发表于 2017-3-21 10:27 | 显示全部楼层
难得一见的好帖
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-5 19:34 , Processed in 0.093820 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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