1 of 10

GDC Top DOwn RPG

Lesson 2: Enemies

2 of 10

Lesson overview

  • We’re going to create some basic enemies!

3 of 10

Setup

  • As you’ve gotten used to know, drag in a sprite of an enemy you want into the scene view
  • You may need to set the sorting layer to 1 (above the ground) to see the sprite

4 of 10

Components

  • Now that you have the enemy in the scene, we have to add the components
  • Similarly to the player we must add these components to the player
    • RigidBody2D
      • Remember to Freeze Z rotation
      • And set Gravity Scale to 0
    • CircleCollider2D
  • [!] We actually forgot to add a CircleCollider2D to the player last time, so make sure to do that!

5 of 10

Enemy Script

  • Create a new script component to the Enemy GameObject, and name it “Enemy

6 of 10

Scripting!

Now this script looks like a lot but we’ll go through it.

If there's anything you don’t understand go get Laurier or Winfred or look for some documentation

7 of 10

The Variables

  • Here in the variable section we have a reference to the player, rigidbody2d (rb) and the speed
  • We are using these to store values for later use in the script

8 of 10

Start Method

  • The Start method initializes some of the variables we’ll use
  • You might see a familiar face of GetComponent<>() which grabs the component from the gameobject (which is the enemy)
  • FindObjectOfType<>() is a function that looks throughout the whole scene for a PlayerController type which is our player— this is how we get a reference to the player

9 of 10

Update

  • The Update method first sets the velocity of the enemy to move toward the player
    • If you’re interested in the math involved in the first statement, feel free to ask Laurier or any officer on Discord or yell for us during a meeting :)
    • Vector subtraction yay
  • The second statement handles the enemy flipping whenever moving in a direction

10 of 10

DONE!!!1

  • You know should have an enemy that follows you around
  • Please feel free to ask questions to an officer, I (Laurier) would be actually happy if you ask me a question about the lesson