unity3d 在Unity 2D游戏中使用“Physics.OverlapSphere”检查半径范围内是否有物体,以便向敌人发出警报

yquaqz18  于 2023-03-03  发布在  其他
关注(0)|答案(1)|浏览(163)

正如标题所描述的,我目前遇到了一个问题,“Physics.OverlapSphere”没有将任何东西放入数组。我觉得这好像是一个非常简单的错误,但我找不到。
下面是我写的代码,我所期望的是当玩家进入Phyhsics.OverlapSphere的半径范围内时,它会添加到数组中,然后触发其余的检查。

public class EnemyFieldOfView : MonoBehaviour
{
    public float efovRadius = 4f;
    [Range(0, 360)]
    public float efovAngle = 45f;

    public GameObject playerSprite;
    public bool alerted;

    public LayerMask playerMask;
    public LayerMask objectMask;

    public Collider[] playerSearch;

    public void Start()
    {
        StartCoroutine(searchRoutine());
    }

    public IEnumerator searchRoutine()
    {
        WaitForSeconds wait = new WaitForSeconds(0.25f);

        while (true)
        {
            yield return wait;
            Searching();
        }
    }

    public void Searching()
    {
        playerSearch = Physics.OverlapSphere(transform.position, efovRadius, playerMask);
        if (playerSearch.Length != 0)
        {
            Transform enemyTarget = playerSearch[0].transform;
            Vector2 targetDirection = (enemyTarget.position - transform.position).normalized;

            Debug.Log("In fov circle");

            if (Vector3.Angle(enemySprite.transform.up, targetDirection) < efovAngle / 2)
            {
                float distanceBetween = Vector3.Distance(transform.position, enemyTarget.position); 
                if (!Physics.Raycast(transform.position, targetDirection, distanceBetween, objectMask))
                {
                    Debug.Log("Alerted");
                    alerted = true;
                }
                else
                {
                    alerted = false;
                }
            }
            else
            {
                alerted = false;
            }
        }
        if (alerted == true)
        {
            alerted = false;
        }
    }
'''
0sgqnhkj

0sgqnhkj1#

假设这是一个3D游戏,玩家应该至少有一个基本碰撞器或一个网格碰撞器与凸勾选。非凸网格碰撞器将无法与此配合。如果游戏是2D的(“enemySprite”)那么你最好使用Physics2D.OverlapCircle或其他工具。尽管Z轴与它无关,您仍然可以为2D设置定义最小/最大深度,其中层与层之间在Z轴上有一定距离。
也是小挑剔,你不必检查:

if(alerted == true)
{
   alerted = false
}

只要执行csharp alerted = false;,即使alerted是假的,它也会被分配两次。
更好的做法是,执行以下操作:

public void Searching()
{
    alerted = false;
    playerSearch = Physics.OverlapSphere(transform.position, efovRadius, playerMask);
    if (playerSearch.Length != 0)
    {
        Transform enemyTarget = playerSearch[0].transform;
        Vector2 targetDirection = (enemyTarget.position - transform.position).normalized;

        Debug.Log("In fov circle");

        if (Vector3.Angle(enemySprite.transform.up, targetDirection) < efovAngle / 2)
        {
            float distanceBetween = Vector3.Distance(transform.position, enemyTarget.position); 
            if (!Physics.Raycast(transform.position, targetDirection, distanceBetween, objectMask))
            {
                Debug.Log("Alerted");
                alerted = true;
            }
        }
    }
}

由于搜索不产生控制,它将全部被处理,因此,如果你仍然有人在范围内,它将保持警报设置为真。

相关问题