4 Replies - 4644 Views - Last Post: 25 April 2012 - 02:42 PM

#1 hiden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-March 12

xna. how to make "mobs"/enemies move towards my character

Posted 04 April 2012 - 12:21 AM

hello

how do i move my mobs towards the caracter they should kill(how to make the mobs follow the character)

like in boxhead, minecraft or allmost all games with mobs.


thank you for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: xna. how to make "mobs"/enemies move towards my character

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: xna. how to make "mobs"/enemies move towards my character

Posted 04 April 2012 - 12:23 AM

I'd check the X/Y co-ordinates of the mob objects (the mob should be an encapsulated class) - then say if the X is greater than subtract from it by using a new point to set the location, all while checking if the mob has walked into another block/object...
Making sense?
What have you tried?
Was This Post Helpful? 2
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,431
  • Joined: 23-August 08

Re: xna. how to make "mobs"/enemies move towards my character

Posted 04 April 2012 - 03:41 AM

Moved to XNA
Was This Post Helpful? 0
  • +
  • -

#4 DanielLeone  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 177
  • Joined: 04-February 12

Re: xna. how to make "mobs"/enemies move towards my character

Posted 06 April 2012 - 03:15 AM

If your just talking 2D, like a top-down, I'd recommend this :
http://www.berecursi...n-object-in-xna

Or maybe in you're into videos, watch some of these :
http://www.youtube.c...IcwqV4CO2PzDcw=
(there are more on his channel if you search [Game AI]).

If you're talking 3D, then sorry I can't be of any assistance (never touched on it before).

Generally on D.I.C you have to post what you have tried or what you have attempted to try.

Hope I helped,
Daniel,

This post has been edited by DanielLeone: 06 April 2012 - 03:16 AM

Was This Post Helpful? 0
  • +
  • -

#5 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 560
  • View blog
  • Posts: 1,262
  • Joined: 24-April 12

Re: xna. how to make "mobs"/enemies move towards my character

Posted 25 April 2012 - 02:42 PM

View Posthiden, on 04 April 2012 - 12:21 AM, said:

hello

how do i move my mobs towards the caracter they should kill(how to make the mobs follow the character)

like in boxhead, minecraft or allmost all games with mobs.


thank you for your help.


So, for beginning you can just move the mobs towards your character while obeying the rules of collision detection. I assume your mobs or character can't fly in this world, if it's 3D. That would make it basically a 2D problem even if you're in a 3D environment since all movement towards the character is really done on the x,z plane.

The simple answer is (Y will actually be Z on the 3D axis):

//Move mob closer to character every frame
if(MobX<CharX) {MobX += MobSpeedPerFrame};
if (MobY<CharY) {MobY += MobSPeedPerFrame};
if (MobX>CharX) {MobX -= MobSpeedPerFrame};
if (MobY>CharY) {MobY -= MobSpeedPerFrame};

This is the most basic "following algorithm" you could use. If you make the mobs follow the rules of collision detection and move them once per frame (like in the Update method) then you should get some good "beginner" results.

The problem is that they are just making a "bee line" straight for your character and are going to get stuck acting like zombies probably sooner rather than later. You'll very quickly run into problems like where there's a wall in between the character and the mob and the mob just keeps banging their head against the wall trying to continuously go through the wall (which collision will never allow) rather than trying to find a way around the wall.

One super easy way to deal with this is to make them "smarter zombies" by making them walk off in another random direction every time they get stuck or feel their way around the edges of the object until they discover a way around it. Even this can still cause them to act pretty stupid if there are complex corners and angles to work around to get to the character.

If these methods don't work, then you need to get into an area of Artificial game Intelligence known as Path Finding. A good book on game AI will probably explain it, but it's mathmatical algorithms to figure out the best path to get to a specific place with numerous objects blocking the direct path.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1