Enemy chase player? (RESOLVED)



  • UPDATE: Organized & Updated post, in case community members pose the same question, they will see resolution quoted below:


    @RobinsonX said in Code for emery chasing you:

    @SavageBuilderKid You can have a timer in the enemy that would constantly trigger the following:

    • Get the position of enemy and player
    • If enemy's x position < player's x position:
    • Move right. (You can use set velocity, apply force, move by, etc. depends on what you are doing.)
    • Otherwise, move left.

    This is a very simple method to emulate an enemy chasing the player. Let me know if you need more!


    ORIGINAL POST:

    I am trying to conduct a study of enemy AI behavior. In my case:

    • Player is running away from enemy
    • Enemy is chasing the player <— this is where I’m stuck

    I created a simple scene that mimicks the donkey kong gif below:
    A9525CFE-CFC8-4272-B51B-6B9D4C1B80A7.gif

    except, in my game scene, donkey kong chases mario throughout the different floor levels. No other elements.

    I read this excellent Enemy A.I. tutorial from hyperPad:

    It teaches how to have the enemy, attack a player within “x” proximity.

    It also explains

    • tag system
    • get position behavior

    These logic systems, provides the enemy with player coordinate awareness. Wonderful! but how do I get the enemy to chase the player?

    That question, is where I’m stumped, any nudge in the right direction will help me tons. Thank you



  • @dumbTactics as a quick guess, I suppose you can use the move to point behavior. You may have to keep track of all player positions in an array and move the enemy to each point after a certain amount of time using a timer behavior.

    For basic player follower functionality you can use a Timer, Get Position, and Move to Point behavior to move an enemy to a player position every ‘x’ amount of seconds.



  • To move a physical object, you can use Set Velocity or Apply Force. Set velocity is great to getting the object to the exact x and y velocity you want - however, since you can manipulate velocity, you have the risk of having your object clip through walls if it's going fast enough. Apply force is great for making your object move naturally as if another player is controlling it. This also makes the object less likely to clip through walls because the object's velocity isn't being replaced but rather added / subtracted.



  • The x and y velocity / force will push the object to move.
    X = Horizontal
    Y = Vertical

    You can use If behaviors to compare the position between the enemy and player.

    Example:
    If player X is greater than enemy X -> enemy moves right


Log in to reply