Physics2D.Linecast ends at first collision

Hi,

I’m trying to implement some security cameras in my game. If the player sprite is under the camera, he should be safe. All other players in the line of sight should be noticed by the camera. I check the collisions with

hit = Physics2D.Linecast(sourcePoint, LookAtPoint, Physics2D.AllLayers, -2f, -2f);

My problem is now, that the ray of the Linecast end at the first collision. All player sprites have the z layer -2. If a player sprite is behind another, than the camera get no hit for him. To activae a camera a player must be at the same position as the camera, so he is blocking the line of sight for this time. Is there a way to get all hits on the full length of the line?

The image shows the Linecast as red line. The white line shows that the Linecast ends at the first collision. In this case, if another player sprite was in the field below this sprite it won’t be detected.

Try using Physics2D.LinecastAll().

Linecast only returns the first collider hit. LinecastAll returns an array of all colliders hit. Loop through the resulting array and check if each collider is the one you want.

You are wanting to use LinecastAll if you want to hit everything in the line.