0 votes

Hi, I am making a top down shoter and building an AI enemy that chases the player and shoots.

But one of the modes of the game allows the bullets to bounce off walls. How do I make AI calculate the bouncing shoot to hit the player and how can I coordinate that to the AI movement chasing the player?

Thanks

Godot version 3.0
in Engine by (19 points)

1 Answer

0 votes

vector.bounce(normal)
it returns a vector in the bounce direction.
the normal is to indicate the direction of the wall. you can give the actual normal of the wall. or just provide the direction you like.
for instance

velocity = velocity.bounce(Vector2.RIGHT)
bullet.position += velocity 
by (27 points)

Just to make sure I understand.

I have something like this:
enter image description here

The yellow line is the path for the enemy AI (red) to catch the blue player. The white walls are "bounceble". Right now the shots already use vector.bounce for the shot.

My question is how to think about making the ai understand the distance to player and walls in the way and fire a shot solution that would take advantage of the bouncing walls to ry to hit the player? I mean, I can make the AI do random shots in the general direction of the user and hope for the bounces but I was looking at the kind of strategy we humans would make, trying to calculate the bounces and catch he enemy off guard.

you can give the enemy, two raycast nodes.
since the raycast node gives you the point of colistion (get_collision_point()). and a vector of the direction of the collision (cast_to). so, when the first_raycast hits the wall. you do 2 things.
1. position the second_raycast in the get_collision_point() of the first_raycast.
2. set the cast_to of the second_raycast to be the bounce direction of the cast_to of the first_raycast

second_raycast.cast_to = first_raycast.cast_to.bounce(wall_normal)

now, if the is_colliding of the second_raycast is hitting the player (get_collider()). you shoot. when the bullet hits the wall, you bounce() his velocity
(p.s. the picture you attached is not showing)

Sorry, I updated the image. hope it's visible now.

Thanks for the reply and explanation. I still haven't tried Raycast2d. Will try this as soon as I can.

I imagine I will need to do many calculations to find the best shot, so maybe need to run a range of angles to see which one connects, right?

i think the raycast system makes calculations unnecessary. since the corridors are narrow, and the player is fat in comparison. so if there is a bounce solution for the shot it will not be very sensitive to tiny variations in the angle. but you can also make multiple ray span, like a fan, for each step of the double raycast system. covering a angular range.

but, depends on your node arrangement and rotation system. i suspect you will have to be aware of global and local space (also for the wall normals). i suspect, it will be easier to calculate the direction from first raycast to the wall collision manually.(i.e not with the cast_to, which is local space, so doesn't change with rotation of the player)

alternatively, you can place many raycast "eyes" on strategic spots on the walls. and if the "eye" is able to cast a ray to the enemy and to the player at the same time. it means there is a bounce shot solution. so you send the enemy a "shoot" signal, with the appropriate angle (which is the angle from the eye to the enemy at the moment of the event.)

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.