Home > Net >  How could I make a collider in unity 3d not collide with my player, but still collide with raycasts?
How could I make a collider in unity 3d not collide with my player, but still collide with raycasts?

Time:05-11

How could I make a collider in unity 3d not collide with my player, but still collide with raycasts? 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.

CodePudding user response:

You can also use the isTrigger or raycast layerMask option to do this. 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
        }
    }
}
  • Related