
In C# you put a “.” and type out the data it is your wanting access to. For example, in a tool belt you access your tools by taking something out of it. Adding a “.” after the transform in C# is the equivalent to accessing something it has. In the above script we are accessing the “ transform” of the player as it has the data we want to manipulate. Either drag and drop it onto the player or go to the player, click “add component” in the inspector and type the name of your script.


Make sure your Player script is attached to your player before you continue. Now we know that we need to access that position through code. The players Transform component holds the data for the players position, rotation and scale. You wanted to change something about the players current position. You clicked on the player therefore having all of their data to work with. So imagine it as a hierarchy going from top to bottom. Earlier in the inspector I had you set the players x y and z to 0, 0, 0. To do that through code we need to access something that holds the players position and tell the computer we want the player to be placed at 0, 0, 0 on the x, y, and z axes. W can add a line of code to make sure the player always start's in that position even if we accidentally moved them while in editor mode. The end result of the script I’ll be showing you gives you the ability to move up/down/left/right and… You can either copy the code from start to finish and call it good but if you want to understand how it works I’d recommend reading through.

Either way it will have plenty of useful information you can take and apply to your project. This is made in the context of a Galaxy shooter where the enemies come at you from the top of the screen. There will be brief explanations of some of the topics I’m going to go over but the point of this article is not to be exhaustive. You will also want to know what a variable and method are. This will assume you have some basic Unity and C# knowledge like knowing what the difference between Start() and Update() are in a script. There is a lot to cover in this article and my goal it to help you understand how to make a basic 2D character controller in Unity as fast as possible while also explaining how it works! This will take you through step by step on some very important features of C# that you will use a lot and give you a solid foundation of some of its core concepts. More importantly, commit core concepts of programming with C# in Unity to memory. Objective: Create a simple 2D character controller for a space shooter style game. Day 6 of Game Dev: Simple 2D Player Movement in Unity!
