Devlog #04: Smarter AI and ENEMY WEAPONS!!
This devlog adds a very nice feature: You can DIE now >:) The AI ship has now got access to a cannon and shoots whenever it looks at you. Along with a gun, the AI has also got a decent IQ boost to make it more challenging to defeat.
The AI’s smartness increase:
I have decided to add a state machine for the AI, which allows it to point directly at the player, shooting while flying towards them, then pull away just before colliding. This is also known as a Boom and Zoom technique
It achieves this with enum State { ATTACK, ZOOM} where the ATTACK is the ‘boom’ part and ZOOM is obviously the ‘zoom’ part. When the AI gets within 200 units of the player, it switches from ATTACK to ZOOM and flies towards a point 500 units above and 500 units in front of the AI, as seen here:
if global_position.distance_to(player_position) >= 200 and state_timer.is_stopped() and current_state == State.ZOOM:
current_state = State.ATTACK
state_timer.start()
elif global_position.distance_to(player_position) < 200 and current_state == State.ATTACK:
current_state = State.ZOOM
state_timer.start()
target_position = current_transform.origin + (forward_direction * 500) + (upwards_direction * 500)
AI Weapons:
For the AI ship’s weapon, I pretty much just copy and pasted the player ship’s cannon. In fact, the only thing different, was making a new scene for the enemy’s bullets to allow them to be on a different physics layer.
Changes To The Player’s Ship
There is now a health indicator bellow the speed in the top left corner when piloting the ship, and when it reaches zero it destroys the player’s ship. I also needed to add 2 new CollisionShape3D for the player’s ship to collide with the enemy’s bullets, along with an extra, much bigger one that causes the enemy ship to shoot when it’s RayCast3D collides with it.