Day 43 — Improvements: new enemy behaviour

Hey and welcome!

Currently our enemies still don’t pose too much of a threat so let’s add in the ability for our enemies to aggressively try to ram the player when they’re within a certain distance. Create a new function or enter the following code directly into your update function:

private float _ramSpeed = 8.0f;
private float _rotationSpeed = 250.0f
Vector3 vectorToTarget = _player.transform.position - transform.position;
Vector3 rotatedVectorToTarget = Quaternion.Euler(0, 0, 180) * vectorToTarget;
float distance = Vector3.Distance(_player.transform.position, transform.position);
if (distance < 5.0f && transform.position.y > _player.transform.position.y)
Quaternion targetRotation = Quaternion.LookRotation(Vector3.forward, rotatedVectorToTarget);
transform.position = Vector3.MoveTowards(transform.position, _player.transform.position, _ramSpeed * Time.deltaTime);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, _rotationSpeed * Time.deltaTime);
transform.rotation = Quaternion.Euler(0, 0, 0);

Right there’s quite a bit going on here, our first local variable vectorToTarget is getting the vector between the player and the enemy. When we have that value we’re then rotating that vector by 180 degrees on the z axis which will stop our enemy from exiting the 2d plain.

Next up we’re getting the distance between our player and enemy by making use of a new tool called Vector3.Distance(). This returns back a float value so we can check when that float is below 5.0f signalling that the enemy is pretty close and then execute the code to move towards the player. We’re also checking here if the transform.position.y of the player is greater than the enemy since we don’t want our enemy ramming the player from behind as there’s no defence for that.

In the if statement we’ve got a new class here called Quaternion.LookRotation() this is looking for two values, the first one is a vector that defines the direction to look in which thankfully can be done pretty easily with Vector3.forwards since we’re wanting the enemy to look directly at the player as it head towards them. The next value is another vector but this one determines what direction up is for the object which we’ve managed to do earlier with our rotatedVectorToTarget variable. It’s a bit complicated but the short of it is that setting the player as the target that our enemy needs to rotate towards while also defining what is forwards and up for the enemy.

Now we’re onto the last new class which is Vector3.MoveToward this is a nice easy one to grasp as it simply moves one object towards the other by taking in their position values, in this case the transform.position is our enemies position that we’re moving towards the player’s position and we’re using _ramSpeed to determine how quickly the enemy moves.

Finally our last bit of code is what actually rotates our enemy by altering the enemies rotation and setting it to rotate towards our player by using targetRotation that we defined earlier. Like the MoveToward we can also set the speed in which the enemy rotates.

Then when the if statement is false the rotation for the enemy is reset so that the enemy will be looking in the correct direction as it moves down the screen past the player.

Now our game is starting to get a bit of challenge to it!

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.