0 votes

I want to add covering for my AI Bots at 3D FPS. How to detect cover places in which bot will be invisible for player?

I want that bot detected that cover places (for standing, sitting or lying) depends on players position and FOV and then run to it and attack the player from it.

How can I do this in Godot?

asked Jul 2 in Engine by Robotex (110 points)

1 Answer

+1 vote

First of all, you need 2 points, one is the head of the player and one is the head of the bot (in the simplest case).
In the code, I will call thoses 2 points respectively player_pos and bot_pos, you also need the direction the enemy is looking (bot_rotation). All three are Vector3. The rotation will be in the same "format" as godot's Spatial (doc) (YXZ-Euler angles)

Then, to have the FOV correct, you have differents solutions, those are two of them:

  1. Calculate a vertical and horisontal angles (code here). With this you can do the next step only if the angles are in certain ranges (and thus have full control over the FOV).
  2. Create a Area(3D) and do the next step only if the player is in this area (it allows for more complex vision shapes)

Once this step is complete, you need to do a ray-cast from the bot to the player using a RayCast node (doc) or the space_state interface (doc). If the raycast collides with a wall or a terrain element, the bot does not see the player, otherwise it sees him.

If you want a place for the player to hide in like bushes in Assasin's Creed: you can put an Area(3D) around the bushs that collides with the RayCast and thus stop it like a wall.

There is also a great video from GMTK explaining how it might work in stealth games which is similar to fps game detection: link.

I hope this helps.

answered Jul 16 by Siandfrance (39 points)

If the raycast collides with a wall or a terrain element, the bot does not see the player, otherwise it sees him.

You didn't understand. The task is hiding the bot from player, not player from bot.

I need to find positions where bot can hide from player's shooting. If bot sees player, it will run to cover and start to attack player from it (moving from cover and back if needed). The biggest problem is changing cover position when player moves. For. i.e. bot can find cover, find the path to it and start running but player is also moving during bot's moving. So, when bot arrives to the end of path the cover position was moved to the other position.

So, I can detect covers but I can't guarantee that cover will be valid when bot arrives to it.

Oups sorry.
A solution would be to take choose a cover that is reasonably far from the player, so wherever he moves it is still valid.
You could also hard code cover places depending on the player's position (I think that this is the most used solution).

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.