I wasn’t trying to insult you. I was just pointing out that a problem like this can be broken into several smaller steps that can be addressed one by one. I want to know what step you are having problems with so I can help you. If it’s the first step, that’s okay. I just need to know where in the process you are. I’m not an expert, but I’ve been working on my own version of Laser Defender for awhile and have been playing with advanced flight patterns over the last couple weeks.
At some point, your enemy is instantiated off-screen somewhere, likely above the top of the screen. In my game, I have a Vector3 point called “destination” and an enemy is flying toward that point. When they reach that point (or just get close), their status can change from “Arriving” to “SideToSide” causing them to begin their side to side movement. You can do this by continually changing the coordinates of the “destination” point every time they get near it. When they get close, call a method that changes “destination” to a new point on the other side of the screen. The critieria for the new destination point is based on your Enemy’s current state (Arriving,SideToSide,Diving,HasDived,etc.)
Using this system, I’ve been able to dynamically tweak the flight patterns of all my enemies in code instead of relying on pre-determined flight paths. This allows for complex maneuvers like you are describing.
At some point, perhaps on a timer, or just when the player’s X position is close enough, they can begin a dive toward the player. “Destination” will be changed to the player’s position. If they miss, they will pass the player and fly toward the bottom of the screen. Getting them to actually turn around and fly back toward the top of the screen is a bit challenging. Rotations give me a slight headache. It’s not that hard (nothing really is after awhile), it’s just another thing to learn and I’ve been focusing on other areas. It’s hard to do by code, but it could be done easier by fussing with the animator once you are used to how it works – which likely won’t be until after the next section, Glitch Garden.
An easier solution to turning around would be just to have it continue flying off the screen, and then move its position back to its place above the top of the screen and change its state back to “Arriving”. (Essentially, you “teleport” the ship back to its original place.) If necessary, you can add a “HasDived” boolean to make sure it doesn’t dive again.
To move toward something, you need three things. First, you need a vector going from your current position to another point or position.
vector3 VectorToDestination = destinationPoint - transform.position;
//destinationPoint can be another Vector3 you’ve constructed or could be the transform.position of another object.
This vector3 will represent both direction and distance. You’ll need to be comfortable with separating those out. A vector3.magnitude will get you the distance, and a vector3.normalized will get you the direction.
float distanceToDestination = VectorToDestination.magnitude;
Vector3 directionToDestination = VectorToDestination.normalized;
With the direction and a float to represent your speed, you have everything you need to fly “toward” something. You can use the distance to continuously check when you are close to your destination (so you can decide on your next destination and start the process over).
transform.position = directionToDestination * Speed * Time.deltaTime;