- Home /
Raycast ignore layers except
I am trying to make it so that, if a player clicks a valid place on the game-world, a selected character will move there. So I am raycasting and grabbing the location where it hits as a target destination.
However, I have had problems with the raycast hitting other things like scenery, and other characters, rather than ignoring them until the ray hits the ground (which is the location that I want).
I tried this: float range = 50; Ray raycast = camerad.ScreenPointToRay(Input.mousePosition); RaycastHit hit;
if (Physics.Raycast(raycast, out hit, 8)) {
if(Input.GetMouseButtonDown(0)) {
selectedPosition.transform.position = hit.point;
}
}
... and then put the map tiles into Layer: User Layer 8 - "Walkable"
However, now all clicks are ignored. What am I doing wrong? Thanks!
RaycastAll can be used to raycast through players scenery etc and then just look for the object tagged Ground or whatever you are looking for.
Answer by giorashc · Apr 03, 2016 at 11:19 AM
This is how you should call RayCast
if you want only colliders from the "Walkable" layer to be hit:
Physics.Raycast(raycast, out hit, range, 1 << LayerMask.NameToLayer("Walkable"))
This will return true only if the ray hit an object from the Walkable layer.