After settling on a rough design for the game, as was described in my last post, I started developing the game in Unity. The first thing I did was get an object moving right, with the camera following it, to represent Icarus. I then added a physics body to the object. As long the user touched the screen, Icarus would move upwards but when they stopped touching the screen, he would fall due to gravity acting on the body. This can be seen in the gif below. The lovely stickman is my own original artwork. At some point I may attempt to make an animated sprite myself or ask someone who can actually draw to do it.
The next step was making Icarus fall when he collided with something. To do this; each frame before Icarus moved, I checked a flag to see if Icarus’s wings were still intact. If they weren’t, Icarus would stop flying right and the player would no longer be able to make Icarus move upwards by touching the screen. This would leave Icarus at the mercy of gravity. In order to set the ‘wings intact’ flag to false, I used Unity’s OnTriggerEnter2D method. This method is called whenever the physics body overlaps with a 2D collider, that is set to act as a trigger. As you may have noticed, there is currently nothing for Icarus to collide with. Let’s change that by adding some water for Icarus to avoid.
To create water of varying height, I used this procedural landmass generation tutorial by Sebastian Lague as a starting point: https://www.youtube.com/playlist?list=PLFt_AvWsXl0eBW2EiBtl_sxmDtSgZBxB3
The tutorial uses Perlin Noise to create terrain similar to how I created the terrain in Anxiety. However, this method uses Unity’s own Perlin Noise functionality to create a 2D heightmap. The results are far more impressive and efficient than the terrain generation I’ve done previously. For my water, I generated a 1D heightmap with Perlin Noise. I then made a rectangular, 2D mesh and used the heightmap to adjust the Y coordinates of the vertices along the top edge of the mesh. I then adjust the colour of the mesh by setting the colour of each pixel to a different shade of blue, depending on the value of the pixel’s Y coordinate. Now for Icarus to collide with the water rather than simply pass through it, I needed to add a collider. Since the shape of the water repeatedly changes, I couldn’t use a standard box or circle collider. I needed something that I could easily adjust to match the mesh geometry. Unity has a component called an EdgeCollider2D which is a line that can be constructed out of an array of 2D coordinates. By storing the top vertices of my water’s mesh, I could easily create a line that followed the edge of the waves perfectly.
Now, in order to make the water change as Icarus flew over it, I needed to keep creating new water meshes below the player. Lague’s tutorial also covers how to make an infinite terrain by creating and updating chunks of the terrain around a viewer. I used a similar method to make my water continuously change with the player’s x coordinate. I generated three water meshes and stored them in an array. These meshes were placed next to each other in the game so that when Icarus flew past one, the next one would take its place on screen. After a mesh had moved across the screen and off camera, it would be removed. A new one was then created to the right of the other meshes. This creates the effect of a continuous sea.
As you can see in the following gif, I now had my waves that could bring Icarus to his demise.
To create water of varying height, I used this procedural landmass generation tutorial by Sebastian Lague as a starting point: https://www.youtube.com/playlist?list=PLFt_AvWsXl0eBW2EiBtl_sxmDtSgZBxB3
The tutorial uses Perlin Noise to create terrain similar to how I created the terrain in Anxiety. However, this method uses Unity’s own Perlin Noise functionality to create a 2D heightmap. The results are far more impressive and efficient than the terrain generation I’ve done previously. For my water, I generated a 1D heightmap with Perlin Noise. I then made a rectangular, 2D mesh and used the heightmap to adjust the Y coordinates of the vertices along the top edge of the mesh. I then adjust the colour of the mesh by setting the colour of each pixel to a different shade of blue, depending on the value of the pixel’s Y coordinate. Now for Icarus to collide with the water rather than simply pass through it, I needed to add a collider. Since the shape of the water repeatedly changes, I couldn’t use a standard box or circle collider. I needed something that I could easily adjust to match the mesh geometry. Unity has a component called an EdgeCollider2D which is a line that can be constructed out of an array of 2D coordinates. By storing the top vertices of my water’s mesh, I could easily create a line that followed the edge of the waves perfectly.
Now, in order to make the water change as Icarus flew over it, I needed to keep creating new water meshes below the player. Lague’s tutorial also covers how to make an infinite terrain by creating and updating chunks of the terrain around a viewer. I used a similar method to make my water continuously change with the player’s x coordinate. I generated three water meshes and stored them in an array. These meshes were placed next to each other in the game so that when Icarus flew past one, the next one would take its place on screen. After a mesh had moved across the screen and off camera, it would be removed. A new one was then created to the right of the other meshes. This creates the effect of a continuous sea.
As you can see in the following gif, I now had my waves that could bring Icarus to his demise.
At this point; I still needed a score that increased over time, along with a sun that could melt the wax of Icarus’s wings. More on that in a later post.