Enemies: Ramming speed

Objective: Create an enemy that rams the player when it gets within range.

Some enemies now try to ram the Player.

The Enemy script controls enemy behavior. The variables shown below will be used to control the enemy behavior to try to ram the player. The _enemyID is assigned using Random.Range(0, 4) and if it is 3 then will ram the player if in range, _playerDetectRange.

Variables required to ram player in Enemy script.

The _rigidBody of the enemy will be used to control movement, and needs to be assigned and null checked.

Assign _player and _rigidBody in void Start of Enemy script.

Before checking the distance from the enemy to the player, the _player and _rigidBody are checked in void Update() because both can be destroyed. If the player is in range of enemy with an ID of 3 then the _attackPlayer Boolean is set to true and used to initiate ramming behavior. To ram the player _rigidBody physics are used to both rotate the Enemy and change its velocity. If the player is not in range the Enemy moves down the screen.

C# code to allow enemy to ram player in Enemy script.

The player now must worry about a dangerous enemy that hunts the player down if they get too close.

Software engineer