How do I make RayCast stop going through walls?

I’m creating a ray from my camera to the middle of my player (of layer ‘Player’) and if there is a wall between the my camera and the player, I am still registering a HIT for the player. It was my understanding( and apparently wrong ) that the ray stops after it collides. The wall does have a collider on it (working too, the player can’t go through it). Here’s my ray code:

RaycastHit[] hits;    
hits = Physics.RaycastAll(cam.transform.position,(playerTargetPosition - cam.transform.position).normalized, myGameCamera.ViewDistance);

for(int i = 0; i < hits.Length; i++) {
	RaycastHit hit = hits*;*
  • if(hit.collider.name == GameConstants.CHARACTER_ID) {*
  •  return true;*
    
  • }*
    }

How do I tell the ray to stop if it hits the wall first?
By the way, the wall is part of the Layer “Default”, but even when I change it to a custom one like “Obstacle”, there’s no difference. Any help would be greatly appreciated!
Thanks
EDIT:
Do to an misread, or possibly a wrong selection with intellisense, the issue is resolved by changing the ray cast call. For those who have made the same unfortunate mistake, here is the changed version:
RaycastHit hit;

if(Physics.Raycast(cam.transform.position,(playerTargetPosition - cam.transform.position).normalized, out hit, myGameCamera.ViewDistance)) {

  • if(hit.collider.name == GameConstants.CHARACTER_ID) {*
  •  return true;*
    
  • }*
    }

Gotta read the docs :stuck_out_tongue:

That function is explicitly built to not stop after it hits something. Every other Raycast function stops after a hit. :stuck_out_tongue:

< Raycast docs >