Thursday 2 April 2015

Unity line of sight is not the same as "clear to proceed"

Another minor stumbling block this evening, following on from our Unity box collider problems. This time it's not the syntax or Unity programming language that's the problem, but our approach to solving a particular problem.

The issue is this:

Currently we're drawing a single line-of-sight from centre-to-centre of each square. If there are any obstacles between the two selected points, we say that there is no line of sight. That's fine - most of the time....


Here, our hero is blocked from walking diagonally, because it will cause him to walk through the edge of the wall. The routines are correctly reporting that there is an obscured line of sight to the target square.

But what if there was an object (or an enemy character) in the square? If we place the main camera on the character's head, in 3D space, we can see the board "through their eyes". And we can clearly see (at least partially) the squares on the diagonals.


We're pretty sure that if one of those green squares contained an enemy character, we'd at least expect to see part of it. So our single line-of-sight routine is going to fail us - at present, it'll simply report a wall in the way of the target square, and as a result we'll end up not putting the partially visible enemy character in the target square.

What we really need to do is measure the line of sight from two points on "either side" of the character, to two points "either side" of the centre of the target square. If either one of these lines is obscured, we don't want to allow our character to walk to the target (since at least part of the path is obscured). However, if either of the two lines is clear, then this means that the character can at least see part of the target square - and so, has an uninterrupted line-of-sight to the target.


In the diagram above, we created two rays, offset slightly from the centre of the originating square, to points either side of the centre of the target square. The green ray passes through the wall (and our routine correctly identifies that the wall is in the way.

However, because the red ray reaches its destination unhindered, we can report a line-of-sight between our character and the target square. If any enemy character were in this square, it should be displayed and placed on the board.


No comments:

Post a Comment