How do I create an entity that patrols items and follows the player in Construct 2?

Not favoritedFavorited Favorited 0 favourites
  • 4 posts
From the Asset Store
Basic Rounded Vector Geometry Player Design with Glow for 3 player games
  • Can anyone help me implement an entity in Construct 2 with the following behavior?

    1. Patrolling random items

    - The entity should randomly choose an item scattered around the map to patrol.

    - It should move to the item and patrol around it for a random time (between 20-50 seconds).

    2. Following the player:

    - If the player enters the entity's field of view or radius, the entity should start following the player.

    - The entity should stop following the player when they leave the field of view or radius.

    3. Item switching:

    - After the patrol time is over, the entity should pick another random item on the map and start patrolling it.

    - This process of choosing and patrolling should repeat indefinitely.

    Additional Notes:

    - The player needs to collect the items, which get destroyed upon being touched. Therefore, the entity should avoid following items that have already been collected.

    - The same item sprite appears throughout the entire map.

    - The game is top-down.

    I've tried to do this on my own, but the entity barely moves, and I'm getting stuck. Any tips or examples on how to achieve this in Construct 2?

  • The way you do this in C2 is not much different from C3. Use LineOfSight, Pathfinding and Timer behaviors. On Timer event change the entity state. Use LineOfSight to check if the player is in sight. If not - pick a random item in range and find path to it.

    An example of logic:

    Initially the entity is in "idle" state.
    
    On "idle" timer: 
     -> Check if the player is in LOS. If true, find path to it, set state to "chase"
     -> Else : Pick a random item, find path to it, set state to "patrol"
    
    On Path found : move along path
    
    On Failed to find Path : set state to "idle" and start "idle" timer
    
    On Pathfinder arrive and state="patrol" : set state to "idle" and start "idle" timer
    
    If state="chase"
    Every 1 second : 
     -> Check if the player is in the attack range. If true, then stop and attack
     -> Else: check if the player is in chasing range. If true, find new path to the player
     -> Else: set state to "idle" and start "idle" timer
    
    If state="patrol"
    Every 1 second :
     -> Check if the item still exists. If not - set state to "idle" and start "idle" timer
    
    
  • The way you do this in C2 is not much different from C3. Use LineOfSight, Pathfinding and Timer behaviors. On Timer event change the entity state. Use LineOfSight to check if the player is in sight. If not - pick a random item in range and find path to it.

    An example of logic:

    > Initially the entity is in "idle" state.
    
    On "idle" timer: 
    -> Check if the player is in LOS. If true, find path to it, set state to "chase"
    -> Else : Pick a random item, find path to it, set state to "patrol"
    
    On Path found : move along path
    
    On Failed to find Path : set state to "idle" and start "idle" timer
    
    On Pathfinder arrive and state="patrol" : set state to "idle" and start "idle" timer
    
    If state="chase"
    Every 1 second : 
    -> Check if the player is in the attack range. If true, then stop and attack
    -> Else: check if the player is in chasing range. If true, find new path to the player
    -> Else: set state to "idle" and start "idle" timer
    
    If state="patrol"
    Every 1 second :
    -> Check if the item still exists. If not - set state to "idle" and start "idle" timer
    
    

    Thank you for trying to help! I followed your suggestion, but it didn’t work out very well. This is my first time creating an "artificial intelligence" for an enemy, and I ended up getting a bit confused along the way. If you could take a look and explain what I might have done wrong, I’d really appreciate it!

    https://www.dropbox.com/scl/fi/6f0p7w4zn05j3i1w8wzm3/Entity-Test.capx?rlkey=nm2gu8lhz06am57tkh1em1r4c&st=7xbhoku6&dl=0

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Pathfinding behavior only works inside the layout bounds, so you need to make the layout bigger. There are a few other issues as well:

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