C# 检测具有相似名称的最远目标
我想得到最远的物体的位置,它与前面的其他物体同名。 我制作了一张简单的图片来说明我的问题:C# 检测具有相似名称的最远目标,c#,unity3d,C#,Unity3d,我想得到最远的物体的位置,它与前面的其他物体同名。 我制作了一张简单的图片来说明我的问题: 我发现了有关RaycastAll的信息,但由于某些原因,我无法获取感兴趣对象的位置。根据您从何处获取匹配的名称以及如何确定光线原点,以下内容应该适用于您。这假设光线是由运行此方法的游戏对象投射的,并充当光线的原点和要匹配的名称 public void GetFurthestObject() { // Replace this with whatever you want to match wit
我发现了有关RaycastAll的信息,但由于某些原因,我无法获取感兴趣对象的位置。根据您从何处获取匹配的名称以及如何确定光线原点,以下内容应该适用于您。这假设光线是由运行此方法的游戏对象投射的,并充当光线的原点和要匹配的名称
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;
}
}
}
public void GetFurthestObject()
{
//将此替换为您想要匹配的任何内容
字符串名称匹配=transform.name;
//初始化光线和光线投射全部。根据需要更改原点和方向。
//这假定此方法是从作为光线原点的变换调用的。
射线=新射线(transform.position,transform.forward);
RaycastHit[]点击次数;
hits=Physics.RaycastAll(光线、浮点、最大值);
//初始化最远值
float furthestDistance=float.MinValue;
GameObject-furthestObject=null;
//循环浏览所有点击
for(int i=0;i最远距离)
{
furthestDistance=当前距离;
furthestObject=hits[i].transform.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;
}
}
}
public void GetFurthestObject()
{
//将此替换为您想要匹配的任何内容
字符串名称匹配=transform.name;
//初始化光线和光线投射全部。根据需要更改原点和方向。
//这假定此方法是从作为光线原点的变换调用的。
射线=新射线(transform.position,transform.forward);
RaycastHit[]点击次数;
hits=Physics.RaycastAll(光线、浮点、最大值);
//初始化最远值
float furthestDistance=float.MinValue;
GameObject-furthestObject=null;
//循环浏览所有点击
for(int i=0;i最远距离)
{
furthestDistance=当前距离;
furthestObject=hits[i].transform.gameObject;
}
}
}
刚刚选中,订单上没有保证删除我的评论。刚刚选中,订单上没有保证删除我的评论。