Enemy AI

0 favourites
  • 3 posts
From the Asset Store
Pixel Enemies for SHMUP consists of 45 enemy ship sprites to be used in your game.
  • Hello,

    I've been working for a few hours trying to set up some simple enemy ai.

    Here is what I want my enemy to do:

    1. Wait for 0.5 seconds

    2. Choose to move left or right for 0.5 seconds

    Then I just want this to loop forever.

    Tutorials I have watched have suggested giving the enemy platformer behavior, setting up an instance variable, and setting certain values of the variable to correspond to left, right, etc.

    I first tried doing this (with 1 being sitting still, 2 being left, and 3 being right):

    When variable is 1 > wait 0.5 second

    set variable to 2 or 3

    When variable is 2 > simulate left

    wait 0.5 second

    set variable to 1

    When variable is 3 > simulate right

    wait 0.5 second

    set variable to 1

    This would make the enemy rapidly switch between left and right at inconsistent rates. I'm not sure exactly why this happens. My best guess is that, since the variable is always 2 during the event, it resets it instead of waiting? I'm really not sure.

    Thanks in advance!

    Tagged:

  • No, you should not use "Wait" when programming such things. Use Timer behavior instead.

    On start of the layout 
     : Enemy Start Timer "change_direction" for 0.5s recurring
    
    Enemy on Timer "change_direction"
     : Enemy set variable direction to choose("left", "right")
    
    Enemy direction="left" 
     : Enemy simulate moving left
    
    Enemy direction="right" 
     : Enemy simulate moving right
    
    

    You can randomize their movement if you restart the timer in "On timer" event for random number of seconds, for example random(0.5, 1.5)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • That worked! Thanks! Glad I know about that timer feature now. It will be useful when programming other enemies.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)