In this section, we will guide you through the process of creating your first project in Unity. This is an essential step to get you started with game development using Unity. By the end of this section, you will have a basic project set up and ready for further development.

Step-by-Step Guide

  1. Launching Unity Hub

  1. Open Unity Hub: If you haven't installed Unity Hub yet, please refer to the previous section on installation.
  2. Sign In: Make sure you are signed in with your Unity account.

  1. Creating a New Project

  1. Click on 'New Project': This button is usually located at the top right corner of the Unity Hub interface.
  2. Select a Template: Unity offers several templates to start with. For beginners, we recommend starting with the 3D template.
  3. Name Your Project: Enter a name for your project. For example, "MyFirstUnityProject".
  4. Choose a Location: Select a directory on your computer where you want to save your project.
  5. Click 'Create': Unity will now set up your project. This may take a few moments.

  1. Understanding the Project Structure

Once your project is created, you will be greeted with the Unity Editor interface. Here are the key components you need to be familiar with:

  • Hierarchy Window: Displays all the game objects in the current scene.
  • Scene View: A visual representation of your game world where you can place and manipulate objects.
  • Game View: Shows what the player will see when the game is running.
  • Inspector Window: Displays properties of the selected game object.
  • Project Window: Shows all the assets and files in your project.
  • Console Window: Displays log messages, warnings, and errors.

  1. Adding a Game Object

  1. Right-click in the Hierarchy Window: Select 3D Object > Cube. This will add a cube to your scene.
  2. Position the Cube: Use the Transform component in the Inspector Window to position the cube at (0, 0, 0).

  1. Saving Your Scene

  1. Go to File > Save As: Save your scene with a meaningful name, such as "MainScene".
  2. Save Your Project: Regularly save your project to avoid losing progress. Go to File > Save Project.

Practical Example

Here is a simple example to illustrate the steps:

// This script will be attached to the Cube to make it rotate
using UnityEngine;

public class RotateCube : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        // Rotate the cube around the Y-axis
        transform.Rotate(0, 50 * Time.deltaTime, 0);
    }
}

Steps to Attach the Script:

  1. Create a Script: In the Project Window, right-click and select Create > C# Script. Name it RotateCube.
  2. Open the Script: Double-click the script to open it in your code editor.
  3. Copy the Code: Replace the existing code with the code provided above.
  4. Attach the Script: Drag the RotateCube script from the Project Window to the Cube in the Hierarchy Window.

Running the Scene

  1. Click the Play Button: Located at the top center of the Unity Editor. Your cube should now rotate.

Exercises

Exercise 1: Create a Sphere

  1. Add a Sphere: Right-click in the Hierarchy Window and select 3D Object > Sphere.
  2. Position the Sphere: Place it at (2, 0, 0).

Exercise 2: Modify the Rotation Script

  1. Modify the Script: Update the RotateCube script to also rotate the cube around the X-axis.
  2. Test the Changes: Click the Play button to see the updated rotation.

Solution for Exercise 2

using UnityEngine;

public class RotateCube : MonoBehaviour
{
    void Update()
    {
        // Rotate the cube around the X and Y axes
        transform.Rotate(50 * Time.deltaTime, 50 * Time.deltaTime, 0);
    }
}

Summary

In this section, you learned how to create a new project in Unity, add and manipulate basic game objects, and write a simple script to interact with these objects. You also practiced saving your scene and project. This foundational knowledge will be crucial as you progress through the course and start building more complex projects.

Next, we will dive deeper into the Unity interface and explore its various components in detail.

© Copyright 2024. All rights reserved