Day 38 — Improvements: enemy movement and ammo display

Connor Fullarton
3 min readApr 22, 2021

Hey and welcome!

In this one we’re going to be looking into implementing side to side movement to our enemies as well as a quick update to the ammo UI display.

Let’s start off with the enemy movement, so far we’ve got the enemy doing vertical movement so it’s a case of adding some horizontal movement as well to that which is pretty easy to do. So head into the movement method in the Enemy script and update it a little:

private float _horizontalSpeed = 4.0f;
private float _verticalSpeed = 1.0f;
private bool _movingRight = true;
if (_movingRight == true)
{
transform.Translate(new Vector3(1, 0, 0) * _horizontalSpeed * Time.deltaTime );
transform.Translate(new Vector3(0, -1, 0) * _verticalSpeed * Time.deltaTime );
if (transform.position.x >= 9.7f)
{
_movingRight = false;
}
}
if (_movingRight == false)
{
transform.Translate(new Vector3(-1, 0, 0) * _horizontalSpeed * Time.deltaTime);
transform.Translate(new Vector3(0, -1, 0) * _verticalSpeed * Time.deltaTime);

if (transform.position.x <= -9.7f)
{
_movingRight = true;
}
}

First thing to note here is that I’ve added in some logic so that the code knows whether or not the enemy is moving horizontally to the right or not using the bool _movingRight. We’re able to use this bool to change the direction as well whenever the enemy hits the borders of the screen at 9.7f and -9.7f .

If you remember how the transform.Translate works we’re setting the x axis to 1 which sets the direction of movement to the right and setting it to -1 sets it to left.

Pretty cool stuff, although you may have noticed that whenever the enemy spawns in or moves back to the top of the screen you’ll find that they always move towards the right so let’s randomize that a bit by adding this code into our Start() function:

float RandomValue = Random.value;if (RandomValue > 0.5f)
{
_movingRight = false;
}
else
{
_movingRight = true;
}

Random.value is just used to return a random float between 0f and 1f so we’re effectively checking for a 50/50 chance on whether or not to set the _movingRight variable to true or false. You can also add this into the movement function in the if (transform.position.y < -5.3f) statement to also randomise the direction when the enemy gets moved from the bottom of the screen to the top.

Ammo visual update

Lastly before heading off lets quickly update our ammo text logic so that it displays as currentAmmo/maxAmmo on the screen as you may have noticed in the gif above. So back in our UIManager script let’s update it a little:

[SerializeField]
private int _maxAmmo = 15;
_ammoText.text = "Ammo: " + ammoCount + "/" + _maxAmmo;

Pretty easy stuff but it’s nice to have the visualisation available. We’re also serializing the field here so that designers have an easier time updating the max ammo if needed.

That’s all she wrote, nice short one here compared to the last one but the next feature I’m implementing is going to require a few more words to talk about.

--

--

Connor Fullarton

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.