The Daily Click ::. Forums ::. Klik Coding Help ::. Enemy AI in TGF
 

Post Reply  Post Oekaki 
 

Posted By Message

KevinHaag



Registered
  28/01/2005
Points
  266

VIP Member
12th February, 2007 at 22:27:10 -

This is what i'm trying to do...

I want it so when the player gets close enough to an enemy, the enemy will see him and chase the player. But when the player gets far enough away, the enemy will give up the chase. Anyone?

Thanks in advance...

 
Image
http://www.kevinhaag.ca

Radix

hot for teacher

Registered
  01/10/2003
Points
  3139

Has Donated, Thank You!VIP MemberGOTW WINNER CUP 1!GOTW WINNER CUP 2!GOTW WINNER CUP 3!GOTW WINNER CUP 4!
12th February, 2007 at 22:42:16 -

distance = sqrt((y1-y2)^2 + (x1-x2)^2)

 
n/a

KevinHaag



Registered
  28/01/2005
Points
  266

VIP Member
12th February, 2007 at 22:45:30 -

HUH?

 
Image
http://www.kevinhaag.ca

Radix

hot for teacher

Registered
  01/10/2003
Points
  3139

Has Donated, Thank You!VIP MemberGOTW WINNER CUP 1!GOTW WINNER CUP 2!GOTW WINNER CUP 3!GOTW WINNER CUP 4!
12th February, 2007 at 23:01:17 -


sqrt((y("player")-y("enemy"))^2 + (x("player")-x("enemy"))^2)

Or, since I don't feel like checking right now how to square numbers in TGF:

sqrt((y("player")-y("enemy"))*(y("player")-y("enemy")) + (x("player")-x("enemy"))*(x("player")-x("enemy")))

And if TGF doesn't have the sqrt() function, use a maths extension to find the root.

This formula gives you the distance between the player and an enemy, in pixels. With multiple enemies you'd probably need to set the object focus with Pick One Of or a fastloop method.
So something like this:

sqrt((y("player")-y("enemy"))*(y("player")-y("enemy")) + (x("player")-x("enemy"))*(x("player")-x("enemy"))) < [arbitrary distance]
---Look at player and move or whatever your chase events are.

sqrt((y("player")-y("enemy"))*(y("player")-y("enemy")) + (x("player")-x("enemy"))*(x("player")-x("enemy"))) >= [arbitrary distance]
---Stop moving.

Of course, since you didn't specify what type of game you're making, I can't be specific. If it's just a platformer or whatever it can be done simply by comparing X values. If you need a better answer, supply better information.

 
n/a

KevinHaag



Registered
  28/01/2005
Points
  266

VIP Member
12th February, 2007 at 23:19:39 -

its for a top down shooter


 
Image
http://www.kevinhaag.ca

axel

Crazy?

Registered
  05/02/2005
Points
  4766

Game of the Week WinnerYou've Been Circy'd!
12th February, 2007 at 23:39:42 -

"HUH?"

Are you really 16, Kevin? You should know the Pythagorean Theorem by now.

Image Edited by the Author.

 
n/a

KevinHaag



Registered
  28/01/2005
Points
  266

VIP Member
12th February, 2007 at 23:43:49 -

I'm 16, and I'm not new to pythagora's. That's just not what i'm looking for. let's say the player gets 100 pixels in any direction away from the enemy, and the enemy who has a bounce ball movement sets its direction to the x,y position of the player and starts movement. Then, if the player gets beyond 100 pixels from the enemy in any direction, it gives up the chase. There's an easier way to do it, i've done it before... i just can't remember how I did it. I know I did it with alterable values though... o_O

 
Image
http://www.kevinhaag.ca

Radix

hot for teacher

Registered
  01/10/2003
Points
  3139

Has Donated, Thank You!VIP MemberGOTW WINNER CUP 1!GOTW WINNER CUP 2!GOTW WINNER CUP 3!GOTW WINNER CUP 4!
12th February, 2007 at 23:55:32 -

That's just not what i'm looking for.
That's EXACTLY what the code I just gave you does. You want to find the distance between two points? Use the distance formula. There's probably an extension that can do it too, but filling in an extension function is no more difficult than copypasting the above formula.

Edit:
If you want to do it a really stupid way, put a big invisible circle over the player. I don't recommend that idea, but it's certainly 'easy'.

Image Edited by the Author.

 
n/a
   

Post Reply



 



Advertisement

Worth A Click