Hello, last week I explained how to add a background game object to the camera view in the hierarchy where it overlaps with the previous square game object in the scene. The game scene eventually shows overlapping between the square and background game object when it is in the same position, especially the world position. To fix this issues i mention that it is two step which is the: tweaking the Z-axis transform position and giving a suitable custom number in the sorting order on the Sprite Renderer component of the game object. This will result in the square game object in front of the background game object in the hierarchy.
Today, I was going to give a tutorial on doing basic scripting, especially using C# in the Unity game engine, where the user needs to learn how to code using the Unity system and also open the software of Visual Studio where the code needs to be inserted at those places. In my previous tutorial, especially during the first tutorial, I mentioned downloading the additional software known as an add-on, where Visual Studio is one of the options that the user must have to begin coding in Unity. There are also other options for which software to use with the Unity game engine for coding purpose esepcially in C#.
The first step is to create the script by right-clicking on the Project tab, especially on the Assets folder, where the this organize of all kinds of things are organised, especially for the current project. By pressing those and select create option at the top of the selection and find the MonoBehaviour script that will create those in the Assets folder of the project. Once it is created for you, choose the right name for that script since the automatic name is usually New MonoBehaviour script. I chose the name for this script to be SquareMovement because this name is related to the function of the script itself.

To make the scripts and other types of things in the project more organised, the user can choose to create a new folder for the specific thing, like scripts. Creating a folder makes things more identifiable when there are other types of assets that need be configure so that by creating folder makes it easier to find the asset that is needed with the folder name. After finishing with the organise script, to add the script into the game object is by either dragging the script directly into the square game object component in the inspector so that it is automatically created as a component there. Alternatively, you can just click Add Component in the Square game object inspector and find the Square Movement script.


Now press left-click on the Square Movement script to open it using Visual Studio, where it is automatically located and open the software. The user also has the option to open the script by right-clicking on the script and choosing Open Selection. In the Inspector, the user can also choose to click the three-dot section to edit the script. There are multiple options that can be used to open the script in Unity Game Engine 6. Once opened, there is pop out for window where it showing the Visual Studio logo and brings up the script sections of the visual interface, like the image below.

Now, there is an issue with the name automatically created previously after creating the script on the project. To fix those issues, the user will need to rename the class of the NewMonobehaviourScript into SquareMovement. After the rename, the user will need to save the Visual Studio progress and get back to the Unity game engine 6 version. The software will auto-compile and make those changes after some loading times, so the user will need to wait, depending on the hardware system, when running the software. Below is the image after renaming the script on the visual studio becoming Square Movement script.

Now, to do movement on the game object, the user needs to implement scripting and coding in Visual Studio, where it need face it on right now. Open that up and look at the MonoBehavior class that has been auto-generated, which has the Start and Update functions. These two functions play an important role for the script when it is actually being run after the user presses the play button on the Unity game engine and the current script is being attached to the one game object in the hierarchy. The start function is the first initialisation of the script before the update function is called. The start function is only called once, while the update function is called repeatedly and is the action of the current code there.
Inside the update function is where the user needs to use this movement of the square game object. To do that, the user will need to use the Unity API(Application Programming Interface), where these things eventually come from the Monobehaviour class itself which called Input function that is automatically give a different view of options for the user to choose from. With the new powerful AI assistance that it is exist on Visual Studio upon installing, make the coding much easier, as it also gives the user different options to code different kinds of steps to begin the movement of the square game object.
By accessing the Input class, there is a function there that could give the user certain types of options. For that, I had to choose Input dot Get Axis where it could get the automatically creation of Input in the project settings of the Unity game engine. This input is important because it is the basics to control the movement of a game object and is part of the legacy of the input system in Unity 6. After Get Axis i had an input string of parameters inside the called function of the class, both for the Horizontal and Vertical for that variable that I return in the scripts. I used those variables as a condition when the player is pressing the current configuration in the project settings for the Horizontal and Vertical Axis. Below shows the current variable being called after the Input dot get axis.

Next, use that current variable float value to know the input is currently being pressed in one condition flow only. Use the horizontal input as the first condition of if and the vertical input as the second condition of the else if. By doing this method, one could manipulate the return value usually between minus one and positive one from the neutral value, which is zero, that is not currently being pressed upon. After getting those values, get the transform component of the game object to make some movement through the use of the world position on the x and y axes of the game object.
The automatically generated code in the script had make me easier to use with the scripting, especially in Visual Studio where it is auto-generated for the user. Instead of using the transform dot position, it has given me the transform dot translate, which I usually use when creating games in the third-person perspective dimension. The variable of vector in the unity containing certain functions like right and up, which makes the movement easier using the value one only. The function of Unity Time .deltaTime is the multiplication of time that is used with the game object that represents the current system or hardware the person had to begin playing without more smoothing capability. Below is the current code that is being set up.

Now, in the editor, the user can press play on the Unity game engine, where they are able to interact and make a movement to the game object on the game scenes. This method only works when the player uses the old input system, also called the legacy input system, in the player settings. To change to the legacy input system, the user will need to go to the project settings and, in the player sub-menu of the configuration, change the Input system package to the old input system package, also known as legacy. By changing this and pressing Accept, the Unity editor will auto-restart to make the changes to it. Below is the image of the restarting editor using the legacy input system package.

In conclusion, now the player will be able to move around using the key that had been setup in their legacy input system in the project setting where usually it being default to the key of W, A, S, and D. This is the normal key to move any horizontal and vertical for the input system which is same with the arrow up, down, left, and right in the player keyboard. For the new input manager, I had planned to cover that in the next tutorial sections, Insyallah. Thank you for reading the tutorial on movement for the game object. Until Next Time.






Leave a Reply