简体   繁体   English

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

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

How could I make a collider in unity 3d not collide with my player, but still collide with raycasts?我怎样才能让 unity 3d 中的对撞机不与我的播放器发生碰撞,但仍然与光线投射发生碰撞? I have been using the built in layer Ignore Raycast to allow my raycasts to go through objects, but stop my player from going through.我一直在使用内置层 Ignore Raycast 允许我的光线投射到 go 通过对象,但阻止我的播放器通过。

You can also use the isTrigger or raycast layerMask option to do this.您也可以使用 isTrigger 或 raycast layerMask 选项来执行此操作。 But I think this is a more sensible solution.但我认为这是一个更明智的解决方案。

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
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM