FindingClosestObject
来源:互联网 发布:计算睡眠网络断开 编辑:程序博客网 时间:2024/06/05 19:05
Author: Lars-Erik Jakobsson (save)
Description
This script illustrates how to find the closest transform to another transform. Commonly used to check which object is closest to the player. Should be ready to go for mobile devices too.
Usage
Name this script ClosestCollider.js and assign it to the player transform. From Inspector tweak the radius, masked layers and update frequency.
JavaScript - ClosestCollider.js
#pragma strict#pragma downcastimport System; /* ClosestCollider.js Description: Finding closest collider without using a pool Last updated: 2012-12-28 Author: save*/ var sphereRadius : float = 10.0; //Radius of the OverlapSpherevar sphereMask : LayerMask; //What layers the OverlapSphere seesvar updateFrequency : float = 1.0; //Update frequency of FindClosestCollider-function private var cachedUF : float; //Cached value of updateFrequencystatic var pTransform : Transform; //Cache the Player Transform function Start () { pTransform = transform; RestartInvokeRepeating();} //Finding the closest colliderfunction FindClosestCollider () { if (updateFrequency!=cachedUF) RestartInvokeRepeating(); var pSphere : Collider[] = Physics.OverlapSphere(pTransform.position, sphereRadius, sphereMask); if (pSphere==null||pSphere.Length==0) return; System.Array.Sort(pSphere, new PositionSorter()); DoSomethingWithClosestTransform(pSphere[0].transform);} //Do something with the closest transform, for instance print its name in Consolefunction DoSomethingWithClosestTransform (t : Transform) { Debug.Log(t.name);} //This is just a fancy extra if you want to update the frequency in play modefunction RestartInvokeRepeating () { updateFrequency = Mathf.Clamp(updateFrequency, .01, 10.0); CancelInvoke("FindClosestCollider"); InvokeRepeating("FindClosestCollider", .0, updateFrequency); cachedUF = updateFrequency;} //Sort the closest transformclass PositionSorter implements IComparer { function Compare(a : System.Object, b : System.Object) : int { if ( !(a instanceof Collider) || !(b instanceof Collider)) return; var posA : Collider = a; var posB : Collider = b; return (ClosestCollider.pTransform.position-posA.transform.position).magnitude.CompareTo((ClosestCollider.pTransform.position-posB.transform.position).magnitude); }}
- FindingClosestObject
- C# 文件存在性判断
- RTX腾讯通基本功能
- Openfire 离线消息的处理机制
- 想要将UIButton设置背景色
- 1130-host ... is not allowed to connect to this MySql server
- FindingClosestObject
- mysql绑定参数bind_param原理以及防SQL注入
- Rand随机数产生
- 安装CentOS的建议
- android binder机制之--(我是Server Porxy)
- TableView详细解释
- 查看字符串在不同编码(ASCII、Unicode、UTF7、UTF8、Default、BigEndianUnicode)下的 Hex
- SAP客户合作伙伴关系使用说明
- ProtoBuf 常用序列化/反序列化API