Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(+1)

I really like the AI, how did you get it to detect line of sight?

Hey!

Thank you very much for the reply!

I have two constants: TargetDistance and RetreatDistance.

Then I calculate the direction from the AI to the Target (so the player). I also takes the magnitude of this vector to get the distance between them.

Then if the distance is greater than the TargetDistance I move the AI to the Direction * MoveSpeed, in the other case where the distance is lower than the RetreatDistance, I move the AI in the opposite direction (-direction * MoveSpeed).

Feel free to normalize the direction before moving, but I decided to keep its magnitude as it is to give it this high mobility and unpredictability.

In the final case where ( RetreatDistance < Distance < TargetDistance) I multiplied the Direction with a SinWave effect using (current Time *  SinWaysSpeed) * SideWaysAmplitude) and moved the ai in the new direction.

After this piece of code, I added a Raycast from the AI to it's normalized Direction with a parameter "RaycastRange" to detect the border.

If it hit the border I moved the AI to the Border's normal direction * MoveSpeed

Finally in my shoot method, I just added a Raycast shooting from the Ai to the target with the both Border and Player layer masks.

For this raycast I added a parameter to describe the range of the AI.

If it doesn't hit anything or the first hit element's tag is "Border", I return out of the function to prevent the AI to shoot.


I think that's it!

Don't hesitate if you have any questions, I'll be pleased to answer.

Have a nice day!