Enemies will attack the player only if they are aware of their presence.
Enemies become aware of the player when they see them or when the player touches them.
Enemies follow a set path but can deviate from it to chase the player.
The project includes some basic tricks to make enemies return to their path after chasing the player.
The player takes damage during a collision with the enemy only if the enemy was previously aware of the player’s presence.
If the player quickly overtakes an enemy that was unaware of their presence, that enemy will appear confused for a moment and then attack the player (this is an unintended lucky effect).
This example was born as a response to this other topic but is designed to be highly modular and optimized for scalability. It may not be very beginner-friendly, but if you have any questions or want to try adapting it to your game, I’ll do my best to help you if I have the time.
you create a raycast to a group. In the group there are players and ground.
In the second line you check a boolean variable. I suppose that if raycast “meet” the player, the variable is set as true but I don’t understand where you set it.
Where do you set “is_player” as true ?
Thanks,
J
Hi! At the beginning of the scene I create a boolean object variable called “is_player” with the value “true” for the player object, this happens just once:
… then, from the position of each enemy, I scan with a raycast towards the objects contained in the group “visible_to_the_enemy”, if an enemy finds an object that happens to have a variable “is_player” with a value “true”, then such enemy is seeing the player:
Thank you,
now I understand.
the second line is called only once. So if the player is behind a wall the is_player variable is not populated.
Thank you very much,
J
It’s more like the is_player object variable is just populated once at the beginning of the scene for the player object; then, if the raycast hits an object with such variable (and the value “true” in it) it’s the player object.
am i doing someting wrong?
because when i am in front of the guard nothing happened.
(if the variable has seen the player is true the enemy should pursue and shoot the player)
My example assumes there’s an eyesight point in the guard object. That’s just a name I gave to a [X, Y] coordinate (point) I created in the guard object.
Did you created it and set it to a functional height (less than the player height in the Y axis) in the guard object?
Also, my example assumes there’s a group containing all the objects that should be visible to the enemy (player, walls), and that the player object has a is_player boolean (yes/no, true/false) variable set to true.
Did you set that in your game?
As you can see my example is checking for lots of things I previously set up to identify the objects the guards/enemies should be able to see, and understand which one is the player object.
my eyesight point is less than the player height, and i also have the object group visible to enemy, with the is_player boolean. Everything should work, but it doesnt. However, i see that you save the raycast result in “runtime.shield_bearer.seen_object.x” and “runtime.shield_bearer.seen_object.y”
what does this mean and does it make a difference with x and y