Lesson 2 - Starting to Code in C# (Unity)
Creating a Folder for Scripts
In this case I’m creating a folder under assets and naming it scripts. And this folder will contain all of the necessary scripts you need to make in a more organized fashion.
Creating our First Script
To create a script, do the same thing you did to create a folder. When your script is created you can rename your script by right clicking and press rename. When you are on your script the inspector will display an overview on the script.
I installed a unity plugin in Visual Studio Code. So once the script was created, I’m able to see the script and be able to edit the C# Script inside of Visual Studio Code.
Adding Components
Once your script is created to assign your script to a game object. You can drag and drop the script inside of the game object. But there is another way to assign your script to the game object. Your Script is a component so pressing Add Component you are able to assign your script.
How to Delete the Component
If you mess up when assigning components just press the 3 dots and it will prompt options and just press delete component and the script will no longer be a component.
Going over C# Code
Once you first create a script you will see some of the following. Some packages that the C# scripts is using like System Collections, System Collections Generic and etc. And under that is the Player Controller Class we have created. There is a colon behind the class implementation and that means that we are inheriting from another class from Mono Behavior.
Important Keywords
-
public
-
Public is a C# Access Modifier that lets you access the attributes from outside the class
-
-
class
-
Using the class keyword creates a class that we can use to make our code more efficient
-
-
void
-
Void keywords tells you that it is a method. The void keyword means that the method won’t be returning any values
-
Function we see
-
Start Function
-
Update Function
Inside of the Player Controller Class there are 2 functions the Start and Update function. These functions are really self-explanatory. Where the Start function is a function that gets called in the start of the game. While the Update function is sorta of a recursive function that gets called every frame.
How to write Comments in C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//Vehicle will move forward when game starts
}
}
To write a comment is C# is similar to how we would write it in Lua for Roblox. For Roblox. we type in ‘—’ which creates a comment. But in C# we type in ‘//’ to create a comment similarly in other languages like C++ and Java.
Writing our First C# Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(0,0,1);
}
}
Inside of the code below we are writing our first line of code inside of the Update Method. We are using the transform Component since we want to change the transform property to move our Game Object.
-
transform: Component
-
Transform is the component we want to change in order to make it appear as the game object is moving.
-
-
Translate: Method
-
Inside of the transform component has many methods and the one we want to use is Translate which takes in 3 parameters the float of X,Y, and Z. Since we want it to move straight we want to make the Z value 1 to make it move forward.
-
-
Float X,Y,Z: Parameters:
-
X,Y,Z are known as parameters, parameters are values being passed into a method to be used. In this case we’re passing 0 for X, 0 for Y, and 1 for Z. So it will change the transform component and make the game object move forward in the Z axis.
-
Note: if a keyword is lower case it means that it is a component and if its uppercase it means it is a Method.
Using Vectors in your C# Code
What are Vectors?
Vectors are used in Unity to find the position of a game object. You can use them to help you calculate distance and movement. There are two vector classes, which are Vector2 and Vector3.
Vector3
Vector3.right // (1, 0, 0)
Vector3.left // (-1, 0, 0)
Vector3.up // (0, 1, 0)
Vector3.down // (0, -1, 0)
Vector3.forward // (0, 0, 1)
Vector3.back // (0, 0, -1)
Vector3.zero // (0, 0, 0)
Vector3.one // (1, 1, 1)
Vector2
Vector2.right // (1, 0)
Vector2.left // (-1, 0)
Vector2.up // (0, 1)
Vector2.down // (0, -1)
Vector2.zero // (0, 0)
Vector2.one // (1, 1)
What’s the difference?
The main difference between Vector2 and Vector3 is Vector2 doesn’t have a Z value. Vector2 is used to find a position of a game object in a 2D plain where it only has X and Y. While Vector3 is used in 3D plains where it was a X, Y and Z axis.
Using the Time Class to change the Speed
Okay, now we understand how to use the Translate method using Vector3 and float values. We are going to use the Time class to change the speed of our game object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.forward * Time.deltaTime * 20);
}
}
Code Break Down
In this snippet of code, we use the Time class’s component called delta Time to change the speed of our game object to move 1 meter per second. We multiply a random number in this case 20 to our speed to now make it travel 20 meters a second.
In this picture the Z value is being changed by 20 every second as we see here. If you noticed that the value being changed is a decimal not a whole number. This is because the transform axes are float numbers.
Lesson Recap
In this lesson we went over how to insert assets into our unity game and finally went over the basic tools we can use in unity like Move, Rotate, Scale, and Rect. In the next lesson we will be going over basic scripting in C#.