Learning Unity is fun!

Kuzey Köse
5 min readSep 20, 2021

--

I decided to learn Unity in a couple of months. So, I researched a lot, and I ran into the learn.unity.com website. Unity essentials tutorial is really fun. This tutorial just gives basic about what is 3d, how to use Unity etc. Then, I finished this tutorial and started another one which is a junior programming course. The first part was done and this is the first blog of my experience.

Creating a project is really easy. Junior Programming course starts with basic pieces of information. Directly starts to teach with how you work on Unity. In the first project, It gave a folder for those simple assets. Tutorial wants to import this folder to your prototype project. When the import part ends, you can learn how to use objects and materials inside Unity. For example, you can carry an object in the scene. In the asset folder, the basic environment imported also has an extra asset like vehicles.

End of these, the coding part comes. I think the ‘Hello World’ project is moving object with code. Firstly, the tutorial teaches that creating a ‘scripts’ folder. All scripts will be created in this folder. When you right-click on the Project section, choose to create and choose C# Script. It is like;

Create 'Scripts' folder -> go 'Scripts' folder -> right click -> create -> C# Script

Initial code is writing automatically. Directly creates a public class with a name which is the file name. And also create two functions in this class. One of the functions is Start and another one is Update. You can see the code below.

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()
{
}
}

Then the video series continues with moving some objects with coding. Actually, it is very easy. Unity thought everything for users. Some keywords help us to write some effective codes. In this example, we can use the ‘transform’ class. Inside the transform, the class Translate function helps us to move an object in a specific direction. When the save code and back to unity form ide. It is basic to apply this code to an object. Just drag and drop the Inspector tab.

void Update() 
{
transform.Translate(Vector3.forward)
}

But this is so fast. We need to slow down so multiplication with (Time.deltaTime * 20) affects our speed.

void Update() 
{
transform.Translate(Vector3.forward * Time.deltaTime * 20)
}

Also, I added the Rigidbody component using add component button under the Inspector. This brings physics movements on the object like gravity. And the best time comes, press the play button 🙂 The vehicle directly starts to move straight and the main camera doesn’t follow the car. So, we need to add code to the camera.

Let’s create another script file which name is ‘FollowPlayer’. Here, we need to think about how to camera follows the vehicle. The answer is, the vehicles and the position of the camera have to be close and they need to move together. Thus, getting the vehicle’s position and giving this position to the camera is part of a solution.

// in FollowPlayer script
public GameObject player;
void Update()
{
transform.potision = player.transform.position;
}

When the FollowPlayer script integrates on the camera, the Player section automatically comes because, in code, we create a GameObject player. Then we need to change the car name with vehicle and drag-drop Player box. Now, the camera follows the vehicle because the positions are the same. We need to separate those two.

Open the FollowPlayer and add a new Vector3 for separating distance with vehicle and camera. And change the Update function name to LateUpdate for smoothness. Then let’s go to Unity and press the play button.

// in FollowPlayer script 
public GameObject player;
private Vector3 offset = new Vector3(0,5,-7);
void LateUpdate()
{
transform.potision = player.transform.position + offset;
}

We need to get data from the user to move this vehicle. Also, turning is a significant idea for a car. Let’s add these parts. In tutorial says that PlayerController needs more code for turning. Firstly open the PlayerController script and add the public float turnSpeed and using the transform.Translate() method with Vector3.right * Time.deltaTime * turnSpeed parameter.

public float speed = 20;
public float turnSpeed;

void Update()
{
transform.Translate(Vector3.forward * Time.deltaTime * speed);
transform.Translate(Vector3.right * Time.deltaTime * turnSpeed);
}

Then, the vehicle needs data from the user to move right and left. So, the program has to take input. Input.GetAxis(“Horizontal”) is a powerful method to take any action from the user. In this case, our left and right key is ‘a’ and ‘d’. You can change the horizontal input in Unity or in Code.

public float speed = 20;
public float turnSpeed;
public float horizontalInput;
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
transform.Translate(Vector3.forward * Time.deltaTime * speed);
transform.Translate(Vector3.right * Time.deltaTime * turnSpeed);
}

Now, the vehicle can turn right and left but not looking good because only the x-axis changes the vehicle so can’t do rotating. Also, starting directly without pressing any button. We need to add some Vertical movement too. Let’s start with Vertical, it is the same as horizontal.

public float speed = 20;
public float turnSpeed;
public float horizontalInput;
public float forwardInput;
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
forwardInput = Input.GetAxis("Vertical");
transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput);
transform.Translate(Vector3.right * Time.deltaTime * turnSpeed);
}

and secondly, change the transform.Translate(Vector3.right * Time.deltaTime * turnSpeed) to transform.Rotate function.

public float speed = 20;
public float turnSpeed;
public float horizontalInput;
public float forwardInput;
void Update()
{
horizontalInput = Input.GetAxis("Horizontal");
forwardInput = Input.GetAxis("Vertical");
transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput);
transform.Rotate(Vector3.up, Time.deltaTime * turnSpeed * horizontalInput);
}

--

--