Unity Basics - can-dea/writing-samples GitHub Wiki


About this sample

I wrote this sample in 2022 while learning about game development software by Unity. This sample is written in markdown.


Learn the basics of Unity, including editor features, scenes, objects, components, materials, and physics.

This document is based on Code Monkey's Unity tutorial on YouTube.

Contents

Downloads
Creating a new project
Editor panels
Editor toolbar
Scene camera controls
Adding and manipulating objects
Adding object components
Creating a material

Downloads

To get started, download the Unity Hub and the Unity Editor from Unity's website.

The Unity Hub is an application for managing your Unity projects and installations. The Unity Hub also provides access to learning and community resources.

The Unity Editor is available to download in multiple versions. These include recent releases on which new features are tested, and long term support (LTS) releases, which are more stable.

This document is updated for Unity Hub 3.0.1 and Unity Editor LTS release 2020.3.28f1.

Creating a new project

You create new projects from the Unity Hub. A number of templates are available to set the starting state of your new project. For this simple project tutorial, you will use the 3D project template.

To create a new 3D project:

  1. In the Unity Hub, on the Projects tab, select New project.
  2. From the list of templates, select 3D.
  3. In the Project name field, enter a name for your new project.
  4. Select Create project. Your new Unity project opens in the Unity Editor.

Editor panels

The Unity Editor’s default layout contains six panels. You can create custom layouts by resizing and repositioning the panels.

Panel Description
Scene Shows objects and any other assets in the current scene and allows you to edit the scene.
Game Shows the current scene from the perspective of its main camera.
Hierarchy Shows a list of all objects that exist in the project, organized by scene.
Inspector Shows the properties of the selected object in the current scene.
Project Shows a list of all files in the current project.
Console Shows messages generated by the Unity Editor.

Editor toolbar

This section describes six commonly used buttons for navigating scenes and manipulating objects.

Button Tooltip Description
Hand Tool Allows you to pan around the scene using left-click.
Move Tool Allows you to move an object freely or on directional axes.
Rotate Tool Allows you to rotate an object freely or on directional axes.
Scale Tool Allows you to resize an object freely or on direction axes.
Rect Tool Allows you to resize an object on one axis.
Move, Rotate or Scale selected objects Allows you to move, rotate, and resize an object.

Scene camera controls

In the Scene panel, you can move the camera to get a better look at objects you add to the scene.

Outcome Action
Rotate view from current position Right-click and hold on scene, drag cursor
Orbit a central point in the scene Hold Alt, left-click and hold on scene, drag cursor
Zoom in or out on scene Hold Alt, right-click and hold on scene, drag cursor
Pan around scene Select on toolbar, left-click and hold on scene, drag cursor
Move directionally Right-click and hold on scene, use W, A, S, D, Q, and E keys
Change movement speed Right-click and hold on scene, scroll with mouse wheel
Zoom in on an object Select object in Hierarchy pane, hold Shift, press F

Adding and manipulating objects

To build a new scene, you start by adding objects. After you add an object, you can manipulate its position, rotation, and scale. In this section, you will add a 3D cube object to your project’s default scene and then move, rotate, and scale the cube.

You add objects from the Hierarchy panel. To add a 3D cube, select in the Hierarchy panel to open the menu, and then select 3D Object > Cube. In the Scene panel, a cube appears:

Using the Move, Rotate, and Scale tools on the toolbar, you can manipulate an object in two ways:

  • To move, rotate, or scale on the x, y, or z axis, left-click and hold the red, green, or blue axis handle, and then drag your cursor.
  • To move, rotate, or scale freely, left-click and hold the shape in the center of the object, and then drag your cursor.

The appearances of the red, green, and blue axis handles and center shapes differ for moving, rotating, and scaling objects. When you use the combined tool for moving, rotating, and scaling an object at once, all three variations of the axis handles and center shapes appear for you to use.

Adding object components

Components are object properties that you use to make an object look and behave a certain way. For example, you use components to add meshes and mesh renderers, materials, and colliders.

You add components to an object using the Inspector panel. Each new object you create is assigned a name and has, at minimum, the Transform component, which stores the position, rotation, and scale of the object. Each additional component you add has a set of unique properties you can configure.

In this section, you will create a new object and then add components to turn it into a sphere that rolls off a flat object (a plane).

  1. Create an empty object and name it.

    1. In the Hierarchy panel, select and then select Create Empty.
    2. Ensure the object is selected. To do so, you can select its name in the Hierarchy panel.
    3. In the Inspector panel, enter a unique name for the object.
  2. Add the mesh filter component to give the object a shape.

    1. In the Inspector panel, select Add Component.
    2. Select Mesh > Mesh Filter.
    3. In the Mesh field, select to open the Select Mesh window.
    4. Select Sphere.
    5. Select to close the Select Mesh window.

    A mesh filter adds a shape but no visual. After the next step, the sphere will be visible.

  3. Add the mesh renderer component so that you can see the object’s shape and set its material.

    1. In the Inspector panel, select Add Component.
    2. Select Mesh > Mesh Renderer. The object becomes visible as a pink sphere.
    3. Expand the Materials section so that the field for selecting a material is visible.
    4. On the field in the Materials section, select to open the Select Material window.
    5. Select a material. Optionally, you can create your own material and then select it here.
    6. Select to close the Select Material window.
  4. Add the Rigidbody physics component so that the object is affected by gravity.

    1. In the Inspector panel, select Add Component.
    2. Select Physics > Rigidbody.
    3. Ensure that the Use Gravity check box is selected.
  5. Position the object in view of the scene’s main camera and test its behaviour.

    1. In the Hierarchy panel, select Main Camera. A preview of the main camera’s view appears in the corner of the Scene panel.
    2. Ensure that the object is in view of the main camera.
    3. Select the Game panel.
    4. Select to preview the scene. The object is in view of the camera and then falls.
    5. Select again to reset the object’s position.
  6. Add a sphere collider component to the object so that it can interact with other objects.

    1. In the Inspector panel, select Add Component.
    2. Select Physics > Sphere Collider.
  7. Add a flat object (a plane) for the sphere to collide with.

    1. In the Hierarchy panel, select and then select 3D Object > Plane.
    2. Position the plane object below the sphere object and in view of the main camera.
    3. Using the Rotate Tool, tilt the plane object slightly so that the sphere object will roll off.
  8. Test the interaction of the two objects.

    1. Select to preview the scene. The sphere object falls, hits the plane object, rolls off the edge of the plane object, and then continues to fall.
    2. Select again to reset the sphere object’s position.

Creating a material

You can create a custom material and then select it when configuring an object’s mesh renderer component in the Inspector panel. In this section, you will create a custom material using the default shader. Then, you will customize the albedo property, which controls the material’s texture and colour.

To create a basic custom material:

  1. In the Project panel, select and then select Material. The new material’s properties appear in the Inspector panel.
  2. Ensure the Shader field is set to Standard. This is the default shader for new materials.
  3. Select a texture:
    1. On the left of the Albedo label, select to open the Select Texture window.
    2. Select a texture.
    3. Select to close the Select Texture window.
  4. Select a colour:
    1. On the right of the Albedo label, select the white rectangle to open the Color window.
    2. Select a colour.
    3. Select to close the Color window.
  5. Give the new material a unique name:
    1. In the Project panel, locate the material file, right-click it, and then select Rename.
    2. Press Enter to save your changes.