Day 16 of 100Days with GameDevHQ
This week marks a few milestones for me in my GameDev journey, the first and foremost is that I am and will continue to be a Game Developer for the rest of my life. I have enjoyed my journey of learning so much and if you had read my last post about the live coding challenge you would know that my confidence in this area has been somewhat lacking from the start. This was not because I believed I could not become a Game Developer, but it was merely because I let me insecurities of in-experience rule my thought process. Well I can tell you now with confidence that now only time stands between me and my goal of achieving a programing, or developer job by June of 2021.
The second milestone was my acceptance into an 8 week intensive course put on by the awesome team at GameDevHQ. It has only been two days but I have already learned multiple new things and put them into practice with the current game I am building, but more on that in a minute. First lets take a look at some code that I wrote yesterday…
Picture here is the code I wrote for one of the challenges we would need to overcome. The challenge was to move a GameObject around an obstacle without hitting it and using something called a NavMesh. Being unfamiliar with using a NavMesh I decied to try and make this work using what I knew and already learned. The code on the left is exactly that, where the GameObject will move around the obstacle, but this has to be hard coded in. Imagine if there was 20 obstacles!?! I realized there was no way this would ever be efficient or passable even though it worked in this scenario. But if there was more than one obstacle my code would be a mile long accounting for each and every Vector3 positional change. So I next I looked up the documentation for NavMesh and learned that this is an AI system within Unity that can read a walk-able path and obstacles in its way. Then the computer will determine the most efficient path to its end destination. In order to make this work i learned that I needed to “Bake” a NavMesh which will tell the GameObject what surfaces it can walk on or not. Then I have to define any obstacles with some added components in the inspector, and lastly I needed a defined “Target” or end location. It was almost as if a light bulb turned on, and music sounded from the heavens. This whole time I have been learning the building blocks to make a foundation and now I am going to take that into hyperspace. I have heard this term many times before as I am sure you have as well, but this was one of the first times I have actually seen it in practice.
And now the optimized code that would allow my Game object do move from start to finish and avoid the obstacle. If you’re counting in the picture above it took a total of 9 lines of code including the method call to make the GameObject move to the target, not icluding the “variables” and “bools”. Now picture left we have done the exact same thing with a GameObject, a NavMeshAgent and 2 lines of code! I was simply amazed and so stoked I was able to pick this up so quickly. Below is a video of what I did with this.
Now you might be wondering why would this be so useful? Besides that fact that I now know how to use AI and the possibilities are limitless, I am now able to implement that in my new project. For the 8 week intensive I will be completing a Game called Tower Defense. The premise is simple, the “Mechs” are coming to destroy us, so destroy the “Mechs” before they destroy you.
Pictured above is the start of the project. In order to make this as real world as possible we are not starting from scratch on this project. This is because often times when you are given a project at a company it will be partly done and Its our job as the Dev to problem solve and make it work. Then we pass it off to the art department to make it look all pretty lol. As you can see we have a scene here of a city under siege, and the first challenge is to create a start, and end point for which the “Enemies” need to travel to. In the winding maze we will be adding some gun points or turrets. So I dove right in with all of my confidence and newly learned NavMesh technique. Everything was looking good, and then when I went to “Bake” the mesh, my walkable path was either non-existent or included everything. Neither scenario was very good because, on one the “Enemy” would walk a straight line and ignore the rock piles, and the other my “Enemy” would just get stuck. After a few hours of trial and error, I finally realized that in my scene there were two different surfaces that I was trying to make into a walk-able path. I remembered reading that a NavMesh works best on a level plane. So I created a 3D plane and placed it on top of the roads. and only the size of the area needed. Then to tackle my biggest problem…Making my game object walk only where I wanted it. In the above example it was easy as there was only one obstacle, and then it hit me! I needed more obstacles lol.
So this is what I came up with. I created a series of cubes, and made them static, froze them on all axis, and added whats know as a “NavMeshObstacle” to each cube. This tells the “NavMeshAgent” that is attached to our GameObject to avoid it. Once those were all in place I “Baked” my scene again and as you can see with the white areas I now had a path through the maze. And if you wondering how to get rid of all this clutter and make my scene nice and pretty again? Well that part was easy, as I learned a while back that I can apply textures to object and if you turn the “Skybox into a texture, you can use it like a mirror when applied to GameObjects. So I did this for the white area and then for all of my red cubes, I just deleted them as they were no longer needed. It is however important to note that you should only delete the cubes once you are fully satisfied with the path that your GameObject will walk.
As you can now see, I have a functioning maze where a GameObject will follow the path I have set before it. And for a little taste of whats to come I created a little clip of the Mechs… Mahalo for reading!!!