This lesson continues from Unity Basics – Part 1.

Create the Player

Create an 3D object.

Use Move to put it near the camera and on the road.

Call it ‘Player’.

Add a Rigidbody Component.

*Press Play you’ll see our globe still falls down.

Our Player would also fall down if it was off the road.

Add Script to Move the Player

Let use a script to make the player move and be controllable.

Make a new folder in Assets called Scripts.

In this new folder, Right-Click and choose Create> C# Script.

Now name this new script : ‘PlayerController’

*Make sure you name it correctly and also name it before you do anything else because this can cause problems later

Now Double click the PlayerController.cs Script file and a new window will open up with the text editor.

In my case I am using Visual Studio (this comes with Unity Installer).

Now copy this code from – First Curly Brace to the Last Curly Brace and paste into the new script you just made . Go from the first Curly Brace Bracket until the Last in your script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5.0f;
    public float turnSpeed;
    public float horizontalInput;
    public float forwardInput;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        horizontalInput = Input.GetAxis("Horizontal");
        forwardInput = Input.GetAxis("Vertical");

        //Move the vehicle forward
        transform.Translate(Vector3.forward * Time.deltaTime * speed * forwardInput);

        //Rotates the car based on horizontal input
        transform.Rotate(Vector3.up, turnSpeed * horizontalInput * Time.deltaTime);

    }
}

Press Save.

Now go back to Unity.

Now Drag the Script to the Player Icon, the automatically creates a Script Component.

Lets examine this new Component.

All the visible Options like ‘Speed’, ‘Turn Speed’, “Horizontal Input’ and “Forward Input’ are available in your component window.

In the Code, all these elements have ‘public float’ in front of them. Items that are ‘public’ are visible in the Unity window.

You’ll see some notes written with ‘//‘ this is to help you understand the code.

For more in-depth training on C # please visit Unity and choose the Create with Code tutorials.

In the Unity Window, press play and test your ‘player’.

If you use the keyboard arrows and go UP or DOWN you can move the object FORWARD and BACKWARD.

Press ‘play’ to stop the game.

To make the object move faster increase the Speed.

To make the object turn, add values to the Turn Speed. Adding 100 will make it turn quick, experiment!

The Horizontal Input and Forward Input: when you press play, will display the information your keyboard is sending to unity. This is good so you know it works. If you don’t want to see those options in the Component window you could change the word ‘public’ to ‘private’

Now lets add Script to make the Camera follow our Player

In the Scripts folder, Right-Click and choose Create> C # Script

Add Script to Make the Camera follow the Player

Call this new script ‘FollowPlayer’

Open up the script in the text editor:

Now copy this code from – First Curly Brace to the Last Curly Brace and paste into the new script you just made . Go from the first Curly Brace Bracket until the Last in your script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowPlayer : MonoBehaviour
{
    public GameObject player;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = player.transform.position + new Vector3(0, 5, -7);
    }
}

Go from the first Curly Brace Bracket until the Last.

Press Save

Now in Unity drag the Player icon from Hierarchy to the Camera Follow Player Script box that says Player.

Test it by pressing play.

You’ll see that the camera is following but it might now be pointed in the right location.

If you look at the script you will see that the ‘transform.position’ or the position of the camera is based on the position of the player ‘player.transform.position’ AND an additional value of (0, 5,-7). 

If you moved your Player back to 0,0,0 in its position and put the Camera position at 90,5,-7) it will line up with what your camera looks like in the preview. But in my case we still might need to modify our player. 

The important thing to realize is that these are connected. 

Adjust the Camera positioning as you see fit.