How to make some raycasts ignore a layer and others not?

I’m making a chess-like game, which uses two types of raycast.

Type one is for clicking. When the mouse clicks, it casts a ray from the mouse position to the game world, and gets the object clicked on.

Type two is for detecting the board state. To check what is in a tile position, a raycast is positioned above the tile and fired down, and returns what object it hits.

I want type one rays to ignore the pieces, so you click through to the tile its on top of, without preventing type two from seeing pieces.

How do i tell one ray “Ignore the pieces” and another “Don’t ignore the pieces”?

Put your chess pieces on the ignoreraycast layer and do your type 2 raycast as you would normally. Then for your type 1 raycast pass it the layerMask field of its constructor:

static bool Raycast(Vector3 origin, Vector3 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers);

where you should give it Physics.AllLayers as the argument for layerMask.

I haven’t tested this at all hence why im adding it as a comment

Scribe