Enemy Movement AI

quinn221quinn221 Member Posts: 280

Hi, so I have started building a top down game. I am trying to figure out the best way to have my enemies chase and attack my hero. I have done this on a platform game, but can't wrap my head around on how to do it for a top down game. I want to make sure the enemy is facing the correct direction when chasing the hero...in all 4 directions.

I found this great video, but I am not sure how to modify it so the enemy chase the hero facing the correct direction... I hope this makes sense... I am open to any suggestions.

Thanks!

Comments

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,879

    To make an actor point in the direction it is traveling, use a constrain behavior:

    Constrain: self.rotation To: vectorToAngle( self.Motion.Linear Velocity.X , self.Motion.Linear Velocity.Y )

  • quinn221quinn221 Member Posts: 280

    @RThurman thanks for the quick response...do you happen to have an example file..I am still new to all of this. I have never used the vectorToAngle before

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,879

    Try this demo:

  • quinn221quinn221 Member Posts: 280

    @RThurman said:
    Try this demo:

    Thanks...I will play around with it. Will this let me change out the animations as well..I have a left, right, up and down animation for my enemy. so if the enemy is chasing my hero up...I see the back of my enemy...and if chasing to the left...it will play the chasing left animation...etc....

  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,879

    No I don't think it will help you there. It simply points to the direction it is traveling.

  • quinn221quinn221 Member Posts: 280

    Ok..any idea on how to make the enemy move and have the animation point in the correct direction

  • ArmellineArmelline Member, PRO Posts: 5,332

    Put the different direction animations in conditions tied to the rotation of the actor.

  • mhedgesmhedges Raised on VCS Member Posts: 634

    If it's a simple left-right, it's conceivably simple, as you can flip the image horizontally. If it's a down-shows-front, up-shows-back type animation, then you have to do animations for front and back. Direction can be handled by booleans.

    For example, let's say you did one set of animation for left-right, as there is symmetry. You did a car facing right.

    Car facing right:

    When
    Attribute self.FacingRight = true

    Change Attribute self.Graphics.FlipHorizontally = false

    Otherwise
    Change Attribute self.Graphics.FlipHorizontally = true

    The above is an "if-then-else" in many other languages.

  • quinn221quinn221 Member Posts: 280

    @Armelline said:
    Put the different direction animations in conditions tied to the rotation of the actor.

    Is it possible for you to set me up with a quick demo....I am still little fuzzy on how to make it happen

  • ArmellineArmelline Member, PRO Posts: 5,332

    @quinn221 said:

    @Armelline said:
    Put the different direction animations in conditions tied to the rotation of the actor.

    Is it possible for you to set me up with a quick demo....I am still little fuzzy on how to make it happen

    If you send me suitable animations, sure :D

  • quinn221quinn221 Member Posts: 280

    @Armelline said:

    @quinn221 said:

    @Armelline said:
    Put the different direction animations in conditions tied to the rotation of the actor.

    Is it possible for you to set me up with a quick demo....I am still little fuzzy on how to make it happen

    If you send me suitable animations, sure :D

    Awesome Here is a drop box link to them... back, front, left and right

    https://www.dropbox.com/sh/ajy614jmx9y1fkz/AACZApMJkSVjIYzx44dg8QaGa?dl=0

  • IceboxIcebox Member Posts: 1,485

    @quinn221 You can also do it with motion linear velocity , if you want to keep the rotation fixed.Not the best method but it works.

    create a self booelan call it updown

    if y velocity is > 0 and self.positiony<game.playery-65 then up animation and change self.updown true otherwise change self.updown false ,

    do the same with negative velocity
    if y velocity is < 0 and self.positiony>game.playery+65 then down animation and change self.updown true otherwise change self.updown false ,

    then create a rule if self.updown is false
    1-rule inside if motion linear velocity x is greater than 0 animate right
    2-rule if its less than 0 animate left.

    it will be something like this

  • quinn221quinn221 Member Posts: 280

    @Icebox said:
    @quinn221 You can also do it with motion linear velocity , if you want to keep the rotation fixed.Not the best method but it works.

    create a self booelan call it updown

    if y velocity is > 0 and self.positiony<game.playery-65 then up animation and change self.updown true otherwise change self.updown false ,

    do the same with negative velocity
    if y velocity is < 0 and self.positiony>game.playery+65 then down animation and change self.updown true otherwise change self.updown false ,

    then create a rule if self.updown is false
    1-rule inside if motion linear velocity x is greater than 0 animate right
    2-rule if its less than 0 animate left.

    it will be something like this

    Do you happen to have the project file for this?

  • ArmellineArmelline Member, PRO Posts: 5,332
    edited May 2016

    Looking at those images you don't want to be rotating the actor at all, you'll need to tie it to motion of the actor. Demo incoming.

    What kind of motion will the enemy follow the player with? You have 4 direction animations, so will it only move on horizontal/vertical lines?

  • quinn221quinn221 Member Posts: 280

    @Armelline said:
    Looking at those images you don't want to be rotating the actor at all, you'll need to tie it to motion of the actor. Demo incoming.

    What kind of motion will the enemy follow the player with? You have 4 direction animations, so will it only move on horizontal/vertical lines?

    Thanks so much for the help...I have been working on this for about a week with no results.

    My plan was to make it move in 8 directions, just using the up or right/left for the angle movements (what ever looks more belieavable) animation to make it chase at an angle. I have been trying to set it up so when the enemy get about 300 pixels away it will start the chase...then once it get out of the radius it go back to its normal patrol

    My hero move in 8 directions, so basically I need the enemy to follow and face the correct direction (and animation)

    Thanks again!!!

  • ArmellineArmelline Member, PRO Posts: 5,332

    So it'll chase in any direction, including steep and shallow angles? There's no grid restrictions or anything like that?

  • quinn221quinn221 Member Posts: 280

    @Armelline said:
    So it'll chase in any direction, including steep and shallow angles? There's no grid restrictions or anything like that?

    Correct...if you have any suggestion or improvements please feel free to add them in.

  • ArmellineArmelline Member, PRO Posts: 5,332

    Okay, hopefully this is what you intended. Afraid it's all I have time for, but I think it covers the right bases.

  • quinn221quinn221 Member Posts: 280

    @Armelline said:
    Okay, hopefully this is what you intended. Afraid it's all I have time for, but I think it covers the right bases.

    this is perfect!!!

  • ArmellineArmelline Member, PRO Posts: 5,332

    @quinn221 said:
    this is perfect!!!

    Glad you like it! If I can find the energy I might do a video on the method I used. Will PM you a couple of questions.

  • digitalzerodigitalzero Member, BASIC Posts: 639

    saving this file too @Armelline!

Sign In or Register to comment.