c# - 检测具有相似名称的最远对象
解决方案
根据您从哪里获得要匹配的名称,以及您如何确定光线来源,以下内容应该适合您。这假设射线由运行此方法的 GameObject 投射,并充当射线的来源和要匹配的名称。
public void GetFurthestObject()
{
// Replace this with whatever you want to match with
string nameToMatch = transform.name;
// Initialize the ray and raycast all. Change the origin and direction to what you need.
// This assumes that this method is being called from a transform that is the origin of the ray.
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit[] hits;
hits = Physics.RaycastAll(ray, float.MaxValue);
// Initialize furthest values
float furthestDistance = float.MinValue;
GameObject furthestObject = null;
// Loop through all hits
for (int i = 0; i < hits.Length; i++)
{
// Skip objects whose name doesn't match.
if (hits[i].transform.name != nameToMatch)
continue;
// Get the distance of this hit with the transform
float currentDistance = Vector3.Distance(hits[i].transform.position, transform.position);
// If the distance is greater, store this hit as the new furthest
if (currentDistance > furthestDistance)
{
furthestDistance = currentDistance;
furthestObject = hits[i].transform.gameObject;
}
}
}
推荐阅读
- flutter - 颤振中的每日定期通知
- amazon-dynamodb - dynamodb 高效读取容量
- javascript - 获取nodejs中上传的图片url
- powershell - VS Code 中的默认输出文件存储在 UTF 16 LE 中,而默认设置为 UTF-8
- authentication - Jmeter:登录后我可以需要 100 个用户来检查仪表板吗
- c# - 如何在 Visual Studio 中将 .NET Framework 更改为 .NET Standard/Core?
- azure - Azure OMS 警报 - 将图表作为 OMS 警报的一部分发送到电子邮件
- reactjs - 在 bitbucket 上使用 testcafe 运行 e2e 测试
- javascript - 待办事项列表中的删除按钮 (HTML/JS)
- r - 数据整理:在不使用 R 中的 for 循环的情况下,从不同结构的 data.frame 创建时间序列数据的最佳方法