func get_cover_location(cover):
var mask = 00011 #player and environment
var position = Vector3()
var hidden_positions = []
var cover_points = cover.get_children()
for cover_point in cover_points:
var ray_start = cover_point.global_transform.origin
var ray_end = player.global_transform.origin
var world_space = get_world().direct_space_state
var ray_result = world_space.intersect_ray(position,ray_end,[self],mask)
if ray_result:
if ray_result.collider.name != "Player":
hidden_positions.append(ray_start)
randomize()
var index = rand_range(0,hidden_positions.size())
position = hidden_positions[index]
return position
Okay to explain what Im doing here
first I get the closest cover with the previous method (get_closest_cover()) ie get_cover_location(get_closest_cover())
then I get all the children of that cover location which would just be Position3D nodes
afterward I loop through them to create an interect ray towards the player from its position if the ray doesnt hit the player that mean its hitting the wall its behind then I add it to a list of hidden positions from the player.
now to add variety I make the enemy randomly choose which one to go to.
Note that im using a Navigation for path finding and the point is just the target location for the agent(enemy) to create a path to