Home > Net >  Physics.Raycast hits default layer always
Physics.Raycast hits default layer always

Time:10-12

I would like to do a raycast on my layer called "Solid Terrain" this does not work as it always takes the default layer into consideration.

var iSolidTerrain = LayerMask.NameToLayer("Solid Terrain");
Physics.Raycast(ray, out var hit, 1000, iSolidTerrain);

Executing this will also hit against default layer.

CodePudding user response:

In the documentation of unity it tells us that the third parameter of the "Physics.Raycast" is actually a bit mask so we could explicitly ignore the "Default" layer and filter out the "Solid Terrain" layer by doing the following:

var iDefault = LayerMask.NameToLayer("Default");
var iSolidTerrain = LayerMask.NameToLayer("Solid Terrain");
Physics.Raycast(ray, out var hit, 1000, 1<<iSolidTerrain|0<<iDefault)

CodePudding user response:

According to documentation, Physics.Raycast takes a layer mask, instead of a layer. If you want to reference the layer by its name, you should use LayerMask.GetMask to achieve that:

int mask = LayerMask.GetMask("Solid Terrain");
Physics.Raycast(ray, out var hit, 1000, mask);
  • Related