Rails space simulator enemy movement question

Hello,

I have a small 3d space simulator on rails project (Unity 5.3) that I’m attempting to add enemy AI to. The player’s ship always moves forward on the Z axis. Enemies spawn from straight ahead or from the left or right sides of the screens. When the enemy ship gets within a specified proximity of the player’s ship, they change to an attack state. I’ve been using MoveTowards to bring the enemy ship to the proximity border. Once the ship reaches the border, I start having issues with the movement. I would like for the enemy ship to always stay out in front of the players ship (at least matched speed) and be able to move around on the X and Y axis so that it can attempt to avoid the player’s fire.

My question is, what is the best way to handle the movement when the enemy ship reaches the attack proximity area so that the ship always stays in front of the player and doesn’t fly away (or fall behind the ship)? Should I just have the enemy ship match the speed of the player’s ship and then use the negative value of the player’s ship speed (as the step in MoveTowards) to have the enemy ship move away from the player on the Z axis at the same rate that the player is moving?

Any thoughts/information is greatly appreciated!

this might be of interest:

(lot of types of steering behaviours listed in that series: Understanding Steering Behaviors Code Tutorials | Envato Tuts+ )

edit: oh it’s not unity specific, so the code isn’t copy/paste ready, but it should give you the idea of how to combine several “direction influences” into a result to then apply.

1 Like

Your problem description contains the answer: in order for the enemy to be stationary relative to the player, both ships must have the same speed.

You could set a static target point relative to the player (it could be parented to the player game object). Then you could use a steering techique to make the enemy AI to follow that target point.

1 Like

I think you’ve both pointed me to what I need. Thanks!