How do I make a character run away from an Enemy?

student0438_ecisd_usstudent0438_ecisd_us Member, PRO Posts: 5
edited February 2020 in Working with GS (Mac)

Im making a dungeon crawler type game where the character moves along the x and y axis and i want to make some characters run away from certain enemies or even from the player itself.

How do i make a character run away from another character who is chasing them or just sitting there?

Comments

  • jamie_cjamie_c ImagineLabs.rocks Member, PRO Posts: 5,772

    One way would to be use the Magnitude Function to see how close the two actors are and then move one away from the other. I have a video that explains the Magnitude Function:

    I updated the demo from that video with a "Run away from hero" behavior/rule. In the demo I've attached drag the green hero toward the red enemy and when they are 200 pixels apart the red enemy will move away from the hero.

  • bob loblawbob loblaw Member, PRO Posts: 793

    what jamie said, and if it’s not in his demo, if it’s a playformer, i’d also add into to rule a condition checking if the two actors’ y positions are close, before activating the behaviour.

  • student0438_ecisd_usstudent0438_ecisd_us Member, PRO Posts: 5

    @jamie_c said:
    One way would to be use the Magnitude Function to see how close the two actors are and then move one away from the other. I have a video that explains the Magnitude Function:

    I updated the demo from that video with a "Run away from hero" behavior/rule. In the demo I've attached drag the green hero toward the red enemy and when they are 200 pixels apart the red enemy will move away from the hero.

    Thank you, this will help me. I saw the updated file and i am so confused on how to put the "run away behavior" to the game. This is the first time I've tried understanding codes. Can you make a simplified and visually pleasing set (of the run away rule). I would much appreciate it, thank you

  • UltraLionBluUltraLionBlu Member Posts: 157

    If you check it is the only enemy rule left ON
    It is all about distance
    If the distance of the green hero and the red enemy is less than 200 the red enemy moves in the opposite direction, if the distance is greater it remains still
    So if you want the "clean" project you can cancel the Bullet and Display actors and all the OFF rules in the enemy actor

  • jamie_cjamie_c ImagineLabs.rocks Member, PRO Posts: 5,772

    Hi, everything @UltraLionBlu says will work for what you need. In case you have trouble understanding that I've attached an updated project that has only the 'run away' rule included. That rule is checking the distance between the two actors, hero and enemy. Once that distance is less than 200 the enemy actor will move backward away from the hero.

    Keep in mind this is a very basic demo, just to help demonstrate how to use the magnitude function. You'd probably want to play with it and make something more advanced in your game. For example if you drag the hero toward the enemy from the bottom, it will move to the right, since that is what the move behavior is telling it to do. But in an actual game you might want the enemy to move up, away from the hero and so on.

Sign In or Register to comment.