XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

AI Enemy Shooting

Last post 1/14/2009 4:42 PM by LeeroyJenkins. 6 replies.
  • 7/28/2008 1:25 AM

    AI Enemy Shooting

    Im making a xna project on mario-type game. As you know the enemy like a mushroom just move from side to side without shooting. May I know how can I make them shoot randomly?? I need the codes and method to do that..
  • 7/28/2008 2:40 AM In reply to

    Re: AI Enemy Shooting

    There are two things wrong with your post: A.) Be more specific. Are you talking about mushrooms sprouting out of boxes and how to get them to animate? Or are you asking how to make the direction random? B.) Tell us what you have tried so far. Your last sentence is easily interpreted to mean you are asking someone to do it for you. Most people don't respond well to that.
    Regards,
    Louis Ingenthron
    Fortis Venaliter
    Lead Developer of FV ProductionsFV Productions
  • 7/28/2008 3:11 AM In reply to

    Re: AI Enemy Shooting

    I mean getting an enemy in the mario world to shoot a bullet every X seconds randomly, and I need someone to help me out with some guidelines. Now the enemy in the mario world dont shoot, but only move side to side.
  • 7/28/2008 3:50 AM In reply to

    Re: AI Enemy Shooting

    Hi nooob,

    You could probably just keep track of the total time and then mod it by the number of seconds you want between shots to check if the enemy should shoot:

    float totalTime = 0f;  
     
    update(GameTime gameTime)  
    {  
       totalTime += (float)gameTime.ElapsedGameTime.TotalSeconds;  
     
       if(totalTime % secondsBetweenShots == 0)  
          shootBullet();  

    The shootBullet method would simply create a new Bullet (assuming you have a Bullet class or something like that), or pull one from a pre-defined collection (if you have that instead).  Once the bullet is created, I imagine it would have it's own update method to handle its behavior.

     

     

  • 7/28/2008 10:56 AM In reply to

    Re: AI Enemy Shooting

    I have a similar issue concerning the rate of fire. This is the code I use to determine how often an enemy shoots:

    float thrustTimer;
    float lastthrustTimer;
    float thrustTimeSpan = 3.0f;  //declerations

    thrustTimer = thrustTimeSpan;  //initialization

    thrustTimer -= (float)gameTime.ElapsedRealTime.TotalSeconds;  //update

    if ((thrustTimer - lastthrustTimer) < 0.0f)
    {
    if (thrustTimer < (thrustTimeSpan - 1.0f) && thrustTimer > 0.0f)
    RocketShot(rocketList, enemyship);
    }
    lastthrustTimer = (
    int)thrustTimer;

    This triggers the rocket shot between each second but it makes it too difficult for the player to shoot back so I want it to be between each 2 second interval, does anyone know how to do this? I got this method from the SpaceWar game so I don't really know how it works.

  • 1/14/2009 1:40 PM In reply to

    Re: AI Enemy Shooting

    hey im kinda new to this.
    I was looking at the code there codiak and i have more experience with that then game making.
    I would say that if where it says "if (thrustTimer < (thrustTimeSpan - 1.0f)"
    If you changed - 1.0f to - 2.0f it may lengthen your interval to 2 seconds
    Im not sure it it will work since i am also new to C# and cant read the code to well but i would say its worth a try.
    post and tell if it works please
  • 1/14/2009 4:42 PM In reply to

    Re: AI Enemy Shooting

    Hi Codiak and jkgames-
       I think your code is a little more complicated than it needs to be, and I think you can get away with

    float thrustTimer = 0f, thrustTimeSpan = 2.0f;

    thrustTimer += (float)gameTime.ElapsedRealTime.TotalSeconds;  //update

    if (thrustTimer >= thrustTimeSpan)
    {
         RocketShot(rocketList, enemyship);


         thrustTimer = 0f;
    }

    That should ensure it fires on each cycle once it passes 2 seconds.

    Not sure what lastthrustTimer does or if it's needed...
Page 1 of 1 (7 items) Previous Next