Aggressive Enemy Type in Unity

2D Space Shooter — Phase II: Core Programming Challenges

Dennisse Pagán Dávila
3 min readAug 18, 2021

Objective: Create an Enemy type that will ram and try to chase the player within a given range.

Note: This script is attached to the Enemy.

  1. First, we need to be able to measure the distance between the Enemy Object and the Player Object. Start by getting the Player Game Object Component within the Start function.

2. As best practice, null check for the Player component to make sure there is one present when the game runs.

3. Declare global variables to control the ram

  • Distance: The distance between the two objects. I will explain this variable further in the next step.
  • Ram speed: A multiplier that we add to the regular movement speed in order to make the enemy move faster. I have set a Serialize Field to enable tweaking the value from the Inspector should it be neccesary.
  • Attack Range: How close the Enemy must be from the Player in order to execute the ram action.
  • Ram Multiplier: Adds a speed boost to the ram attack.

4. Now, we need to be able to measure the distance between the two objects. Luckily, the Unity Engine provides us with a built-in way to achieve this through vectors. By using Vector3.Distance, you simply need to provide the other transform(in this case the player), and the transform position the script is currently attached to.

Note: The code from the following steps should be placed within the Update function, or within a function of its own that is called in the Update.

You can tweak the distance of the Attack Range by using this in combination with a Debug.Log to print the exact distance of the objects as they are currently placed in your scene.

5. Make an if-statement to compare the distance to the attack range and execute the ramming action accordingly,

  • If condition: If the distance is the same or less to the attack range, I.E. when the Enemy is close enough to the player.
  • Vector 3 Direction: A new variable to set the direction in which the Enemy will be moving.
  • direction.normalized: A vector is composed of direction and magnitude. A vector’s magnitude is removed when it is normalized, leaving only its direction. Technically, it is converted to a vector with a magnitude of 1. However since 1 is the multiplicative identity, multiplying a magnitude (length) by normalized vector results in a distance equal to that length in the normalized vector’s direction. Therefore, when the normalized vector is added to our transform’s movement calculation, it will move in the direction of the specified object with exact units.

The Result:

This article is part of a series of Core Programming Challenges from GameDevHQ. In the next article, we’ll be taking a look at how to make a color-flicker/blinking effect in Unity!

--

--

Dennisse Pagán Dávila
Dennisse Pagán Dávila

Written by Dennisse Pagán Dávila

Software Engineer that specialize in Game Development. Currently looking for new opportunities. Portfolio: dennissepagan.com

Responses (1)