我怎样才能使 unity 3d 中的对撞机不与我的播放器发生碰撞,但仍然与光线投射发生碰撞? (团结新手!)

How could I make a collider in unity 3d not collide with my player, but still collide with raycasts? (new to unity!)

我怎样才能使 unity 3d 中的对撞机不与我的播放器发生碰撞,但仍然与光线投射发生碰撞?我一直在使用内置层 Ignore Raycast 来允许我的光线投射穿过物体,但阻止我的播放器穿过。

您也可以使用 isTrigger 或 raycast layerMask 选项来执行此操作。但我认为这是一个更明智的解决方案。

public class Player : MonoBehaviour
{
    public Collider[] ignoreColliders = Array.Empty<Collider>();
    public void Start()
    {
        var myCollider = GetComponent<Collider>();
        
        foreach (var _ignoreCollider in ignoreColliders) 
        {
            Physics.IgnoreCollision(myCollider, _ignoreCollider); // to ignore affect on player collider
        }
    }
}