Enemies: Ramming speed
Objective: Create an enemy that rams the player when it gets within range.
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.
The _rigidBody of the enemy will be used to control movement, and needs to be assigned and null checked.
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.
The player now must worry about a dangerous enemy that hunts the player down if they get too close.