Search Unity

Top-down shooter AI...

Discussion in 'Scripting' started by NickCaligo42, Mar 4, 2010.

  1. NickCaligo42

    NickCaligo42

    Joined:
    Oct 15, 2008
    Posts:
    9
    I'm making a simple top-down scrolling shooter (kind of like Ikaruga or Zero Wing) right now. I've got pretty much all the basic functionality of it--the player can shoot, destroy enemies, get killed themselves, etc. and the next step is to actually build a level with functional enemies in it.

    The enemies aren't complicated. I just want them to follow along set paths--lines, really--maybe pause somewhere in the middle of their path, shoot on a timer, and leave, but for the life of me I can't figure out a good way to do it. Here's what I've devised so far:

    1 - Create "waypoints" that're basically just empty gameobjects, number them, and have the enemies follow those in sequence, using a series of empty variables to store the numbers of the waypoints I want them to follow so that I can quickly put in "41, 42, 43, 44, etc." in the editor. At various waypoints I can script it to start or stop shooting.

    2 - Use splines in Maya to create specific animations, then import them and assign them to enemies. Make sure any colliders are attached to the enemy geometry rather than the enemy gameobject.

    3 - Attempt to figure out the exact mathematics of the movements I want to see and do it all programmically.

    4 - Use a series of triggers to "guide" the path of enemy ships. Whenever they hit a particular trigger, they turn 180 degrees while flying. Whenever they hit another particular trigger, they turn 90 degrees. Whenever they hit another, they stop, turn towards the player, start firing, then leave, etc. Either the code is in each specific trigger, or it's in the enemies and tied to triggers with specific tags. Either way the result is the same.

    This is the best I can come up with off the top of my head... does anybody have any other recommendations?
     
  2. zelk

    zelk

    Joined:
    Mar 13, 2009
    Posts:
    246
    Perhaps you could use beizer curves to get more smooth movement: http://forum.unity3d.com/viewtopic.php?t=31412

    Otherwise, you could make your own paths using sine, cosine etc. to get smooth movement.

    Also, to make it generic so that you won't have to make to much scripting on each enemy, you could have some kind of list of movements to complete and when they start in time...

    If each enemy was derived from a base class and the base class looked like something like this:

    Code (csharp):
    1.  
    2. public class Enemy : MonoBehaviour
    3. {
    4.     public  Movement[] mMovements;
    5.     public  string[]   mMovementArgs;
    6.     public  float      mMovementStartTime;
    7.  
    8.     private mCurrentMovementIndex  = 0;
    9.     private mTimeSinceMovementStart = 0;
    10.  
    11.     void FixedUpdate()
    12.     {
    13.         // Add code to find how much time has passed since the current
    14.         // movement started (maybe you need a member variable telling
    15.         // you at what game-time it started).
    16.  
    17.         // Add code to step to next movement if it's time for that... and reset timesincemovementstart to 0...
    18.  
    19.         mMovements[mCurrentMovementIndex].StepMove(mMovementArgs[mCurrentMovementIndex]);
    20.     }
    21.  
    22. }
    23.  
    24.  
    25.  
    Then you could have Movement be a baseclass of all different implemented movements:

    Code (csharp):
    1.  
    2. public abstract class DriveLineComponent : MonoBehaviour
    3. {
    4.     public virtual void StepMove(string aArguments);
    5. }
    6.  
    Something like this maybe... then you can implement lots of different movement methods and use them in your enemies... obviously, the implementation here is far from complete and there are a few design flaws that should be able to find better solutions for... It would be nice if you could find a way to implement this so that you can poke around with movement in the Unity Editor GUI, similiar to the example above...

    Hope this get's you started.

    /Ricky
     
  3. NickCaligo42

    NickCaligo42

    Joined:
    Oct 15, 2008
    Posts:
    9
    Well, it's definitely a start--I'll poke around with what that guy with the flythrough did a bit, because it looks like EXACTLY what I could use. Thanks a lot!