Enemy Movement AI
quinn221
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
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 )
@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
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....
No I don't think it will help you there. It simply points to the direction it is traveling.
Ok..any idea on how to make the enemy move and have the animation point in the correct direction
Put the different direction animations in conditions tied to the rotation of the actor.
Contact me for custom work - Expert GS developer with 15 years of GS experience - Skype: armelline.support
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.
My Blog / App Store / Google Play
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
Contact me for custom work - Expert GS developer with 15 years of GS experience - Skype: armelline.support
Awesome Here is a drop box link to them... back, front, left and right
https://www.dropbox.com/sh/ajy614jmx9y1fkz/AACZApMJkSVjIYzx44dg8QaGa?dl=0
@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?
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?
Contact me for custom work - Expert GS developer with 15 years of GS experience - Skype: armelline.support
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!!!
So it'll chase in any direction, including steep and shallow angles? There's no grid restrictions or anything like that?
Contact me for custom work - Expert GS developer with 15 years of GS experience - Skype: armelline.support
Correct...if you have any suggestion or improvements please feel free to add them in.
Okay, hopefully this is what you intended. Afraid it's all I have time for, but I think it covers the right bases.
Contact me for custom work - Expert GS developer with 15 years of GS experience - Skype: armelline.support
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.
Contact me for custom work - Expert GS developer with 15 years of GS experience - Skype: armelline.support
saving this file too @Armelline!