[Project Week 13] Line of sight

Line of sight

This week i'm going to make sure that my player can't attack the skeleton when he's not facing it and not in line of sight of his target, so when my character is facing away from the skeleton he should not be able to attack at while currently he does.

Code

First I created a bool function that returns true if the player is in LoS of the enemy:



And if the player is not in LoS it returns false:



This line of sight will be working as a ray cast which is a thin line between 2 points, so we can cast an invisible line between the player and the target and if the line hits the target then the player is in view of him, if the line doesn't hit the target we are unable to see him therefor we shouldn't be able to cast a spell.

This line of code calculates the targets direction, so whether the player is facing the target or not:



Here I created so that a raycast is created by the player which is thrown into the direction of the target, and if the ray hits the block (invisible barriers I created) then the player can't cast a spell, if not then return true, so the spell is then cast at the target:



Outcome

This is the outcome of the line of site function, it's hard to tell from the image below but basically if the player is not facing the target and I press space to attack, no spell is cast, but once the player is facing the enemy then a spell is cast. In the grand scheme of things this will make the game harder at it's core as if you could just attack enemies from anywhere on the screen then the game wouldn't be difficult at all, so with this line of sight feature it requires a bit of mechanical skill and thinking to my game.

Comments