Search Unity

Detect if other player (prefab) is visible

Discussion in 'Physics' started by DrMethod, Jan 15, 2020.

  1. DrMethod

    DrMethod

    Joined:
    Jan 15, 2020
    Posts:
    36
    Hi,

    I took the proton-viking-asset, deleted the viking and replaced by capsule-mesh, also replaced the terrain by a empty one with a "wall" on it..

    now lets say there is a player1 and a player2 in the game.. player1 needs to detect somehow, if player2 is in his view-range.. but player2 could also hide behind a wall, so the detection should only succeed if player2 is really visible for player1.

    has someone a idea how to get this managed?
     
  2. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    467
    In case you're be happy with just an approximation and your walls and obstacles have colliders on them, you could try a couple of raycasts in the shape and direction of the other players capsule. If any of those raycasts actually hit the other players collider, he's visible, if not, he's hidden.

    This will, however, not cover edge-cases like a small hole in a wall where, by chance, none of your raycasts hit the other player. You'd get a false negative there.

    An overly complicated workaround for that would be to copy the depth buffer of the visible frame to a render texture, re-render just the opponent with a "z-buffer equal material" and check if any pixels were drawn.. you should do that at a way smaller resolution than the original scene though and maybe not every frame... and things like that are costly, especially since you'll want to read pixels of the render texture back to the CPU-world.
     
    DrMethod likes this.
  3. DrMethod

    DrMethod

    Joined:
    Jan 15, 2020
    Posts:
    36
    I think i give the first approach a try :D
    thanks for the explanation.