【发布时间】:2022-01-21 11:37:32
【问题描述】:
我想检查播放器对象的某个半径内是否可交互。为此,我使用 Physics2D.OverlapCircle 检查玩家附近的 2DColliders。我不太清楚为什么参数 LayerMask.NameToLayer("Interactable") 没有检测到任何东西,尽管该层上有对象。如果删除第三个参数,它会检测到玩家是最近的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CheckInteractable : MonoBehaviour
{
void Update()
{
Collider2D checkRadius = Physics2D.OverlapCircle(transform.position, 5, LayerMask.NameToLayer("Interactable"));
if (checkRadius != null)
{
print(checkRadius.ToString());
}
}
}
To string is not printed despite there being objects in the interactable layer
【问题讨论】:
-
只是为了确定你能添加一个
Gizmos.DrawSphere(transform.position, 5);,看看它是否真的足够接近? (它必须在OnDrawGizmos或OnDrawGizmosSelected中,而不是在Update中) -
尝试
1 << LayerMask.NameToLayer("Interactable")作为最后一个参数