Unreal Engine 4: Space game.

We have been working on creating a simple base for a space game with a controllable player that can move left/right while shooting – but only be able to fire 4 bullets. Enemies spawn from both sides of the screen, and fly across in the direction opposite of what side they spawned from, and can be destroyed by the bullets.

player_bpThis is the player blueprint. The ‘MOVE’ comment box is the basic script we made for moving the character left/right. Before creating this BP, under project settings we were able to add macros for movement. I added A+LeftArrow to have a scale of -1, while D+RightArrow to have a scale of 1. We then take this axis value, and use it to change the velocity of the character along the Y axis – Though multiplying it by the scroll speed variable (Mine is set to 400) to make the speed more reasonable.

The ‘Shoot.exe’ Comment box as I decided to call it contains the scripts for firing the missiles. It takes an input (Space/Left click) and uses a branch to test whether or not the missile count is less than 4. This means that if there are 4 bullets in the scene, you cannot fire another. If it is less than 4, then it will spawn spawn the missile actor BP at the player’s location – then increment the Missile count variable.

In the world itself, the player is spawned at the origin, and to stop the player from drifting off the sides of the screen if they move too far right/left, I have placed two blocking volumes that stop the player from moving too far to the side.

missile_bpThis is a scrennshot from the missile_BP. The top box labelled ‘Gety Playery Referencey’ gets the player reference by finding all actors of the ‘Player’ class and promoting them to a variable to use later on. There should only be one instance of this actor, and that’s the one controlled by the player.

The ‘Losing Calories (Bullets) box simply decrements the missile count variable by using the player reference we just created. Also, we set the missile lifespan to be 2 seconds. After those 2 seconds, the missile is destroyed to free memory – this will trigger the script I just mentioned, and decrement the variable.

The final box will destroy the missile if it collides with another actor. In this situation, that would be the enemies flying across the screen. This works by taking the ‘Other actor’ that the missile’s collision capsule is overlapping, casting it to Enemy_BP, then destroying it. It will then trigger the ‘Increase score’ function which is simply the player reference from before connected to a ‘Score’ variable and an increment node. It will then add 1 to the score by triggering the ‘Add to score’ function from the player_BP by using the character reference (May need revising) before destroying the missile – triggering the previous script that decrements the missile count.

score_function
‘Add to score’ function

There is also a projectile movement component which will cause the missile to fly upwards and not succumb to gravity.

playerThis is a screenshot of the placeholder enemy we created. It features a static mesh (Totally not “Borrowed” from the internet) surrounded by a collision sphere to detect whether or not any missiles are touching it. There is also a projectile movement component which will cause the ship to fly along the X axis. The ship is spawned via a ship spawn blueprint. This blueprint will determine whether or not the ship has spawned on the left side of the screen, or the right. Because the game was built directly on the world origin, this was quite easy – see whether or not the value along the Y axis was above or below 0. Depending on which side the enemy spawned, the enemy will either face left or right, before flying in that direction. So if the enemy spawned on the left, it would face and fly right – Or if the enemy spawned on the right, the enemy would face and fly left. The enemy will also destroy after it leaves the screen by having a lifespan of 3 seconds. The enemy spawn blueprint also determines when the next enemy should spawn. It will spawn as soon as the game is loaded, and then create a variable of the current world time but add 1 to it. When the world time reaches a value that is equal or greater than the variable it will spawn a new enemy and change the variable to the new current time, and add 1 to it – then the cycle repeats. This causes an enemy to spawn once every second. We are using Equal to/Greater than ass opposed to just equal in case, for any reason, the frame in which the enemy should spawn is missed. The enemy spawn BP will also change its position along the Z axis in real time to add some variety. To do this, we used a ‘Random float in range’ node to choose a random value between 200 and 1000, and then change the position of the spawn BP to the chosen number along the Z axis. Because enemies are spawned via the same create actor node as the missile but use the location of the BP to determine where to spawn the actor, because the location of the spawn BP is moving up and down, enemies will also spawn at random heights along the Z axis.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s