Blog Post 1 INTP 362 — C#

Ryan Andres
6 min readFeb 26, 2021

Welcome to the wonderful world of C#. First and foremost, I will start off highlighting some differences between C# and Java. After that, we will shift into the main focus which is using C# for game development. C# is a very well known and used programming language. The most notable C# applications being Stack Exchange, Microsoft and others.

You might be asking yourself what makes C# so special? To me, the answer is very simple. C# is a popular programming language, it allows for a lot of variation with how it can be used; and the syntax is simple and easy to follow. Another question might be what exactly can C# be used for? This programming language has a wide variety of uses. From web applications, to mobile apps, to video games and so much more. As C# was released to be a rival to Java, there are many similarities between the two languages. However, I will precede to point out the differences.

The differences between the two are not that drastic. A difference between the two languages is the Integrated Development Environment (IDE). For Java we were introduced to BlueJ first semester, and since then we have used Eclipse, and NetBeans. For C#, the top IDE’s are Visual Studio and MonoDevelop. Of course the IDE’s/languages will have equivalents of each other (same functionality but different name). For Visual Studio, the ‘using’ (circled in red below) would be the equivalent of Eclipses ‘import’. For example, in the second picture below, you can see that the Console class is defined within System. When using System, we do not have to type ‘System.Console.WriteLine’ to write to the console. Instead, we can write it as ‘Console.WriteLine’. Although this will not throw an error if written either way as seen below, it still allows more efficiency within the language. Now looking at the underlined ‘namespace’, it might be easy to assume that this is an equivalent to packages in Java. However, they are not exactly the same. Packages can organize files, control access and overall makes the use of classes, interfaces and all alike simpler. Whereas a namespace organizes programs and keeps sets of names completely separate from one another.

Another minor difference is that in Java you might declare a String variable as shown in the first image below, with an uppercase letter, as writing it in lowercase would cause an error to be thrown which is demonstrated in the second image.

However, in C# you can write the variable either starting with a capital or a lowercase.

We will now be moving onto the main focus of this blog, using C# for game development. In order to properly develop a workable game at my level, I use Unity as my game engine of choice. Unity is a very well-known and respected game engine for many developers. A recently released massive game made with Unity that emerged in August of 2020 was ‘Fall Guys’. There is truth in saying that this is a reliable and trustworthy engine to use. Unity has some variety, with its most popular programming language being C#, followed by JavaScript.

One of the first things I learned in Unity after seeing how to create objects, was creating a script for the attached object to move based on user keyboard input. For this example, we will code it so the user can move using W, A, S, D keys. This is surprisingly a very easy task to do. I will walk through step-by-step to show you how to do this. For starters, you will need to have an object for this script to be attached to. I chose a 3D capsule which is highlighted in the following image.

After adding in your game object, we will need to attach a ‘RigidBody’ to our game object. A RigidBody in Unity are components you attach to objects in order for the objects to react to real time physics. Now that we have attached that, we can now create a script either in the Asset directory or through the object itself by clicking “Add Component”. Once we create the script, we can open it up. Upon opening it you may notice that the default script is different.

There are two methods Start and Update. As mentioned by the default comments in the code, Start is called before the initial frame update, and Update is called once per frame. These only happen if MonoBehaviour is enabled (underlined in red). MonoBehaviour is the base class for all Unity scripts.

For basic movement, we know that we should work with the Update method because it is based off user input. Calling the method once a frame will handle all this input the best way possible. To start it out, we need to attach our script to our object. This can be done by dragging the script file on top of your game object in the hierarchy. Once that has been completed, we can jump into coding this. For starters we have a line at the top of the script ‘public RigidBody rb’. This is a line made so we can attach an object to our script so the script knows which object it is moving or manipulating.

The next set of code we need to implement is a series of if and else if statements to make decisions based on what key is pressed. An easy way to do this is by using ‘Input.GetKey(Keycode.Key)’ as illustrated below. Breaking down this code, our If statement is attached to the ‘w’ key. Anytime this key pressed, a force is added to the object attached to the rb variable. This force moves the object. A different type of variable that isn’t in Java but is in C# is the Vector3. This variable is used for passing 3D positioning and directions and takes in three arguments which are the X, Y, and Z axis. Now we will look at ‘Vector3.up’. The keyword up is used in this context to manipulate and move the game object upwards as long as it’s pressed. In all games, the w key is universally used for moving an in-game object upwards. By using that same logic we can assume that the a key will move left, d will move right and s will move down.

Using this information, we can add a series of else if statements and apply the same logic but to different keys which will have them moving the object in different directions. That would look something like this:

Movement is a very important aspect of all types of games and there are many different ways it can be implemented. This is a beginner way of dealing with movement and allows a lot of versatility and modification. We can apply this same logic to most in-game objects.

After understanding movement, we can start using this knowledge to begin the basics of a game. In my next blog, I will outline some more advanced features and walk you through a block breaker type game. I have created a video outlining all that was talked about in order to help the visual learners understand these concepts easier. The video is linked below. Enjoy!

--

--