Enemy Movement

As some of you may know, I’m working on my game for cam.dude’s halloween contest. The player is pretty much complete, and I’m now working on the enemies. I know how I want them to work, but I don’t know how to do it. Basically, I want them to move towards the player. I think this can be achieved by saying: if enemy’s x-coordinate is less than the player’s x-coordinates, then add to the enemy’s x-coordinates. The same would apply for the y-axis, and also if the x or y is greater than the player’s x or y. I’m sure this can be done simply with python, but I don’t know much python. Can anyone help me accomplish this? Thanks.

I would use the steering actuator. Set the player as the seek object for all of the enemies. If you have a complex terrain, you can create a navmesh that enables the enemies to move around obstacles.

You should be able to find navmesh tutorials fairly easily.

Here’s a simple script you could use.

import bge

SPEED = .1


scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()


# objects
own = cont.owner
target = scene.objects['TargetName']


# movement vector
vec = target.worldPosition - own.worldPosition
vec.normalize()


# move object
own.worldPosition += vec*SPEED

Thanks a lot guys! Thanks especially to Mobious, for the simple, yet excellent script. It works perfectly!!!

p.s. Soon i’ll post a video of my halloween game, as I now have (almost) completely working enemies.