GameMakerBlog Tutorials
Top Down Game: Make an enemy unit face the player
GM Version: GameMaker Studio 2
Making an Enemy unit face the player or even face a certain direction is a question that shows up on the GameMaker Community forums many times and is a fundamental function in creating any game that has enemy units; no-one wants to see enemies looking the opposite way when they are coming after your player even though that is a rather funny scene to picture. This tutorial will cover the Top Down type of game, where players and enemies are viewed from directly above as opposed to a Platformer that uses a side view; each game type requires different ways of changing, tracking and facing the player unit so we will tackle them in separate posts. The most important goal of this tutorial is not so much the code but rather the “why” and “how” the code is working; in this way the lessons learned here can be reapplied in other situations and carried along as another tool in your developer utility belt.
Top Down
First we need to setup a simple test room for a top down type game using the following parameters:
- Name: rmTopDownExample
- No Sprite for Background
- Background Color Default: Hex 000000
- Width: 1024
- Height: 768
- Clear Display Buffer checked on
Next we need to setup our assets:
- Create two test sprites, one to represent the enemy units and another to represent the player unit , alternatively you can download the two sprites I made and import them into your game
- Create two objects using the player sprite and enemy sprite you just created
- Drag one instance of the player object just created to the middle of the room
- Drag four instances of the enemy object created to surround the player instance in the middle of the room
You should have a room that looks like this now:
In order to start testing if enemies are tracking and facing your player character you will need to add a way for your player to start moving around the map. The easiest way is to add some events to the player object you created:
- Key Down Event: We will use Key Down events so we only need to press and hold down a key for our player object to move; in this case we can use the common WSAD movement keys
- Key Down → Letter A → Set Instance Variable → X Coordinate → -3 Relative → (GML) x += -3;
- Key Down → Letter D → Set Instance Variable → X Coordinate → 3 Relative → (GML) x += 3;
- Key Down → Letter W → Set Instance Variable → Y Coordinate → -3 Relative → (GML) y += -3;
- Key Down → Letter S → Set Instance Variable → Y Coordinate → 3 Relative → (GML) y += 3;
- If you want to use different keys that is fine, just remember that moving the player UP and DOWN requires changing the Y coordinate, where negative numbers make a character move UP and positive numbers make a character move DOWN. Likewise when moving a character LEFT or RIGHT the X coordinate needs to be changed in a way so that negative numbers move a character LEFT and positive numbers move a character RIGHT.
- You should be selecting the Relative option if you are using the Drag and Drop Set Instance Variable component, this moves the player every frame the exact amount of pixels you are adding or subtracting from the coordinate. If you do not use the Relative option, the character will instantly be placed to the coordinate specified which is not what we want to accomplish in this tutorial but could make from some interesting gameplay, such as teleportation.
Now that we have a movable player on the screen we can turn our attention to the enemy units and add some tracking logic to them.
- We are going to use the following function to figure out the angle that each enemy unit needs in order to point towards the player object: point_direction(x1, y1, x2, y2)
- Let’s stop and explain this function very quickly and at a high level
- The first set of coordinates: X1,Y1 refer to the “Observer Object” or in other words the Object that is going to be looking at the Player Object. In our case, it is the Enemy Unit’s X,Y position we use here.
- The second set: X2,Y2 refer to the “Observed Object” or the Object we want to look to at, this case being the Player Object’s X,Y position.
- This function effectively determines what Degrees the first object has to face in order to look at the second object, based on where both object instances are in the Room
- We can also swap the observed and observer positions to generate an interesting effect
- Let’s stop and explain this function very quickly and at a high level
- Now, open up the Enemy object and add a Step event, we will use a Step event so the enemies will update their facing every frame of the game to watch the player move.
- Step Event → Execute Code → image_angle = point_direction(x, y,objPlayer.x, objPlayer.y);
- We use image_angle here because our enemies are not moving
- We can use the objPlayer reference because we only have one but if we had a multiplayer game with 2 or more Player Object Instances we would use a specific Player instance X,Y coordinate instead
- Step Event → Execute Code → image_angle = point_direction(x, y,objPlayer.x, objPlayer.y);
Go ahead and run the project at this point and you should have something close to the playable game below
This is not all you can do with point_direction() however, if we apply it slightly differently above we can get the opposite effect: all enemies facing away from the player character, showing their backs instead; which would make for an amusing scene at say an Inn when your character walks through the door and everyone stops and turns around to snub you.
To achieve that just change the function to the following: point_direction(objPlayer.x, objPlayer.y,x,y);
- Now instead of trying to find what angle the Enemy has to turn to face the Player, we instead find what angle the Player has to turn to face the enemy and we apply that direction to the angle the Enemy is facing. This has the effect of turning all enemies around, perfectly showing their backs.
- You can try this in the game above by pressing 1
There are of course even more uses for this function, some ideas would include:
- Making a compass that always points North or to your Home Base
- Creating Trackers that can track different objects or guide you to the nearest resource
- Simulating a surveillance camera that tracks a player if he gets within range
- Having an NPC audience “watch” as an important NPC gives a moving speech while pacing across a platform
- Shooting projectiles at an enemy
As explained earlier, this tutorial is specialized to a Top Down perspective game and we will explore other ways of adding facing logic to a Platformer type game in another tutorial. Hopefully this will be helpful to anyone starting out game development and wondering how to make a sprite face a player or object.
If there are any questions or requests, please leave me a comment below.
Thank you so much for this, i am pretty new to this coding and i couldn’t make it work, i had done the code right but forgot to add .x and .y in the end of the obj name. also if i could get a quick answer for this, is there a way to then make the enemy to look forward even if the player changes direction and walk towards faced direction. thank you again