Multiple target enemy state machine

Not favoritedFavorited Favorited 0 favourites
  • 10 posts
From the Asset Store
State Machine
$10.99 USD
State Machine is a great tool for managing the state of anything in your game.
  • Im trying to make a game that's like a tower defense and I want you to be able to fight alongside allies but I'm not too sure how to make them Switch target in a certain range and pathfind over to them I want them to target the closest thing to them

  • Use Timer and LineOfSight behaviors. You can have multiple LineOfSight behaviors with different settings - for example one for long range, where the unit needs to move to the target, and another for close range where it can start shooting at the target.

    Use instance variables for states. And NEVER use "Trigger once" condition in state machine code!

    Here is a very simplified example, just to demonstrate the idea:

    On Start of layout
     ----> Ally Start recurring Timer "find_new_target" every 2 seconds
    
    
    Ally On Timer "find_new_target"
    
     Ally has LineOfSightClose to Enemy
     Enemy pick nearest to Ally
     ----> Ally set enemyUID to Enemy.UID
     ----> Ally set state to "attack" 
    
     Else
     Ally has LineOfSightFar to Enemy
     Enemy pick nearest to Ally
     ----> Ally find path to Enemy
     ----> Ally set state to "chase" 
    
  • Wait how do I make "Enemy pick nearest to Ally" do I make the ally pick by UID?

  • woah your instructions helped me make them move towards different things and not at the same thing unless its closer but I'm not too sure about the UID part how does that work?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Not sure I understand your question.

    The parent "On Timer" event will trigger for an Ally instance and will pick it.

    Then the nested "Ally has LineOfSight to Enemy" condition will pick all enemies in the range. And then you find the nearest enemy instance among those that are in range.

    I forgot that On Timer can sometimes trigger for multiple instances, so it's better to add For Each loop:

    Ally On Timer "find_new_target"
    System For Each Ally
    
     Ally has LineOfSightClose to Enemy
     Enemy pick nearest to Ally
     ----> Ally set enemyUID to Enemy.UID
     ----> Ally set state to "attack"
    
     Else
     Ally has LineOfSightFar to Enemy
     Enemy pick nearest to Ally
     ----> Ally find path to Enemy
     ----> Ally set state to "chase"
    

    You can save the selected target UID in the enemyUID instance, in case you need to pick the same enemy in other events, for example to fire a bullet at it.

  • I think I understand all of it now but there's one part I'm confused about "----> Ally set enemyUID to Enemy.UID" how do I make the ally change the enemy UID I'm probably missing something obvious so sorry about that

  • See my previous comment, I edited it

  • man I feel like this is like explaining how to make a sandwich to a caveman for you right now butttt how do I save selected target UID in the enemyUID instance I feel like I should know this but I dont

  • Create an instance variable on the Ally sprite. Then use "Ally Set value" action.

  • OHHH that makes so much more sense tytyty

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