I began implementing the functionality of the game by first allowing the player to control the rocket on the screen. I used a triangle as a placeholder in the sprite renderer, and I also added a rigidbody 2D component so that the rocket is affected by gravity. Next I wrote a script that changes the rigidbody’s velocity to an upwards vector whenever the space bar is held down. However, I found that this felt too limiting, as the rocket would always go up at the same speed.
To fix this, I instead added an upwards force, using the Rigidbody2D.AddForce() functions. This definitely gave it the kick it needed for the upwards movement to feel impactful and more fluid, however, it quickly led to erratic movement as both forces kept rapidly increasing the velocity in both directions. Therefore, I needed to program in a way to get the velocity to rest at, or around, 0 for a bit when the thrusters are activated; this gives the system a reset, stopping the acceleration from compounding and getting out of control. This also makes the rocket float in space for a moment, which can give the player a short breather.
To implement this, I added an if statement that would check for whether there is a downward velocity whenever they are activating their thrusters and, if there is, the rocket will immediately counter it with a direct increase in upwards velocity until it reaches 0. This is essentially like slamming on brakes every now and then to control the system, and by adding this it feels much smoother to play as the player feels like they have more control.
Next I implemented a camera follow which causes the camera to follow the rocket as it goes up and down. However, I still wanted the rocket to have some movement on the screen when it is not moving, or is switching from going in one direction to the other. To do this, I used a script which checks the rocket’s y position relative to the camera’s and to only move up/down with the player when it reaches certain thresholds. The code can be found below.
// Update is called once per frame
void Update()
{
if ((rocket.position.y - transform.position.y) > upperThreshold) { // Calculate the amount between the y coordinates of the rocket and the camera, if it is greater than the upperThreshold value do the following
offset.y = upperThreshold * -1f; // Change the offset for the camera's y axis so that it is not following from directly on the rocket, but slightly below
transform.position = rocket.position + offset; // Move the camera (the offset is added so the camera will still move with the rocket but slightly below)
} else if ((transform.position.y - rocket.position.y) > lowerThreshold) { // Calculate the amount between the y coordinates of the camera and the rocket, if it is greater than the lowerThreshold value do the following
offset.y = lowerThreshold; // Change the offset for the camera's y axis so that it is not following from directly on the rocket, but slightly above
transform.position = rocket.position + offset; // Move the camera (the offset is added so the camera will still move with the rocket but slightly above)
}
}
I have chosen to have the camera and player move, rather than having everything moving around them and having the camera fixed, because I will have a large background with planets at specific points. This does, however, mean that the player will be constantly able to move up higher into the game world, which may cause processing issues at ridiculously high values. I may need to eventually change this, to a system where the world moves around the player, but for now it works fine as it is.
Next I added a floor, to represent the planet surface, and I gave it a Box Collider 2D component. As I had already given the rocket a rigidbody, this meant that the rocket would impact the floor and not pass it.
Finally, I added the fire for the thrusters, so that the player has instantaneous feedback for when they interact with the game. I made the fire as a child of the rocket, so that it follows it. To make the fire invisible, whenever the player is not pressing space, I added code that sets the fire’s scale to (0,0,0) so the player cannot actually see it, even though the object is still there.
A video showing all of this in action, and the game in its current state, can be found below.