如何在没有物理/光线投射的情况下检测附近的GameObject? [英] How can I detect nearby GameObjects without physics/raycast?

查看:178
本文介绍了如何在没有物理/光线投射的情况下检测附近的GameObject?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在以玩家为起点的范围内检测物体. 如何在不使用对撞机或Physics.OverlaptSphere()的情况下从播放器周围的给定区域中找到Transforms,我不想使用此方法,因为我唯一需要的信息是从给定LayerMask(更具体地讲,是位置和旋转).如果我要使用物理学,则必须在我认为不必要的每个点上都设置一个触发器.

I am trying to detect the object's within a range having the player as origin point. How can I find the Transforms from a given area around the player without using colliders or Physics.OverlaptSphere() I don't want to use this method because the only information I need is the Transform of nearby Objects from a given LayerMask (more specifically, the position and rotation) If I would use Physics I would have to put a trigger over every point which I find unnecessary.

还有其他方法可以找到附近的点,但是类似于使用物理学的方法吗?

Is there other method of finding the nearby points other but similar to the one that uses Physics?

推荐答案

如果您希望在没有Physicics或Colliders的情况下执行此操作,请访问所有对象.遍历它们,检查图层,如果它们匹配,请使用Vector3.Distance比较每个对象的距离.返回结果.

If you want yo do this without Physcics or Colliders, access all the objects. Loop through them, check the layer and if they match, use Vector3.Distance to compare the distance of each object. Return the result.

List<GameObject> findNearObjects(GameObject targetObj, LayerMask layerMask, float distanceToSearch)
{
    //Get all the Object
    GameObject[] sceneObjects = UnityEngine.Object.FindObjectsOfType<GameObject>();

    List<GameObject> result = new List<GameObject>();

    for (int i = 0; i < sceneObjects.Length; i++)
    {
        //Check if it is this Layer
        if (sceneObjects[i].layer == layerMask.value)
        {
            //Check distance
            if (Vector3.Distance(sceneObjects[i].transform.position, targetObj.transform.position) < distanceToSearch)
            {
                result.Add(sceneObjects[i]);
            }
        }
    }
    return result;
}

这可以通过使用Scene.GetRootGameObjects检索所有GameObjects来改进,但它不会返回标记为DontDestroyOnLoad的对象.

This can be improved by using Scene.GetRootGameObjects to retrieve all the GameObjects but it does not return Objects that are marked as DontDestroyOnLoad.

扩展为扩展功能:

public static class ExtensionMethod
{
    public static List<GameObject> findNearObjects(this GameObject targetObj, LayerMask layerMask, float distanceToSearch)
    {
        GameObject[] sceneObjects = UnityEngine.Object.FindObjectsOfType<GameObject>();
        List<GameObject> result = new List<GameObject>();
        for (int i = 0; i < sceneObjects.Length; i++)
            if (sceneObjects[i].layer == layerMask.value)
                if (Vector3.Distance(sceneObjects[i].transform.position, targetObj.transform.position) < distanceToSearch)
                    result.Add(sceneObjects[i]);
        return result;
    }
}

用法:

List<GameObject> sceneObjects = gameObject.findNearObjects(layerMask, 5f);

这篇关于如何在没有物理/光线投射的情况下检测附近的GameObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆