0% found this document useful (0 votes)
10 views7 pages

Unity 3D Basics: Creating a Simple Scene

This document presents a tutorial on the basics of Unity 3D. It explains how to create a Unity project, navigate the editor, create a simple scene with cubes and a textured ground, animate the cubes with C# scripts, and manage interactions with the keyboard keys.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Unity 3D Basics: Creating a Simple Scene

This document presents a tutorial on the basics of Unity 3D. It explains how to create a Unity project, navigate the editor, create a simple scene with cubes and a textured ground, animate the cubes with C# scripts, and manage interactions with the keyboard keys.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Option Réalité Virtuelle et Interaction (RVI)

Computer Science Major, 5th year 2016 - 2017

Tutorial: the basics of Unity 3D

[Link]

Documentation : [Link]
Tutorials:[Link]

1. Launch Unity 3D and create a new project


Launch Unity 3D software. A dialog window appears, asking you to sign in with a
user account. You therefore have two options: either you create an account now, or you
choose the "Work offline" option. You will be able to complete this tutorial without any issues by choosing
the "Work offline" option. However, if later you want to retrieve data from the asset store
Unity 3D (3D models, scripts, etc.), you will need an account (it will be possible to create and you
connect with an account later even if you choose the 'Work offline' option now).

• Create a new Unity 3D project by clicking on 'NEW'. Choose 3D, as well as the name and the destination.
of your project. You do not need to load predefined packages for now (you will be able to do it later.
to be done afterwards if needed.

• Save the current scene ([Link]) by giving it a name (File > Save Scenes).

Save your work: project and scenes.


Unity 3D saves information about your project in different ways. This means that
that you must save your work differently depending on the information you have modified:
Lascenecorresponds to the 3D scene containing a hierarchy of objects (GameObject) as well as their
parameters (position, etc.). When you perform a backup of the scene, the project is
also automatically saved. Multiple scenes can be created in the same project.
- The project allows storing information that is not specific to the scenes, such as
for example the project parameters, the scripts, the imported packages, the 'Build' parameters, etc.

1
2. The Unity 3D editor
When you create a project, you get a camera (Main Camera) and a default in the scene.
light to illuminate the 3D scene (Directional Light). The editor window then appears as follows.
in the following way:

This editor window consists of the following elements:


1. The graphic editor of the 3D scene allowing direct manipulation of the objects in the scene.
2. The scene tree allows visualizing the hierarchy of objects present in the scene.
3. The inspector allows you to see the different scripts attached to the selected object (by 1 or 2) and to
modify their settings.
4. The project explorer allowing to see the different components of the project and to add them to the
Scene by a simple drag and drop. For now, there is only the scene in the project.
5. The buttons to launch the execution of the 3D scene in the editor which is very useful for
test and debug the scene (we will see later how to create an executable for the scene).
6. The visualization of the 3D scene from the viewpoint of the virtual camera. The editor toggles.
automatically on this visualization when the scene execution is launched.
7. The console that allows you to see error messages or logs during the execution of the scene.

Interactions in the graphic editor can be carried out using the following buttons:

Voto go[Link] and[Link]


[Link] plus d’information.
2
3. Create a simple scene
To start, we will create a very simple application that displays several blue cubes.
at different positions:

• Insert a plane into the scene to create a floor (GameObject > 3D Object > Plane).
Using the inspector (3), place this plan at coordinates (0, 0, 0) and verify that it is nicely visible.
the camera now. Enlarge this shot by playing with the scale so that it has a length and a width of
100 meters and check that it is clearly visible at infinity when taking the camera view.

• Add a cube to the scene (GameObject > 3D Object > Cube) and place it so that it is
visible by the camera.

• Create a material to give color to the cube (Assets > Create > Material). A
a new component is created in the project (4), give it a name. Then modify its color in
the inspector to obtain the blue of the following image. Then drag and drop the material from
the project explorer (4) on the cube in the scene hierarchy (2). The cube should take the
blue color in the graphic editor (1).

• Add a texture to the plane: download the texturegrid_grey.png from the course site, go to
(Assets > Import New Asset…) and choose the texture. Drag and drop the texture from
the project explorer (4) on the layer in the scene hierarchy (2). Then select the layer in
the hierarchy of the scene (2) and modify the material parameters in the inspector (3) in order to
to achieve the same soil texture as in the image below. In particular, it will be necessary to adjust the number
the number of times the texture is repeated ("tiling").

• Move the cube and the camera to obtain a similar image when you run the scene:

• Add a physical behavior to the cube: select the cube and add a Rigidbody to it.
Observe what happens when you run the scene.

• Drag and drop the cube from the scene hierarchy (2)
towards the project explorer (4): this will create a 'Prefab' at
start from the cube (see blue icon) which you will need to rename
(CubePrefab for example). Then, by the inverse operation,
insert 11 other cubes from this model into the scene and
move the instances of this cube to obtain the same
disposition only in the image on the right.

• Execute the scene.


• Select the CubePrefab in the project explorer (4) and
modify the parameters of his Rigidbody to change its
physical behavior (in particular, the mass 'Mass' and the
friction "Drag"). Execute the scene with several parameters
3
different. What do you observe?
4. Animate an object in the scene
Scripts in C# or Javascript allow us to give behavior to objects. We prefer them.
scripts in C# because this language is closer to Java that you probably know better (typing, etc.).
We will therefore create a new C# script to rotate the cubes we have created:

• Create a new script (Assets > Create > C# Script) that you will name RotateCube.
• Edit it by double clicking on it (by default, it will be opened with Mono).
• The Update() function, which is inherited from the MonoBehaviour class, is called at each step of
time during the execution of the scene. In this function Update(), perform a rotation of 3° according to
the vertical axis (Y). For this, you will be able to access the position of the object to which the script will be associated at
through the attribute transform (already defined in the parent class MonoBehaviour). You will be able to
use the auto-completion of Monopour to see the functions that can be applied to this attribute
Transform to find the function that allows for the correct rotation.

• Add the script to CubePrefab by dragging and dropping it onto the prefab icon in the project explorer.
(4). Note the appearance of the script as a component in the inspector (3): it can be disabled or
delete. Note also that all instances of CubePrefab have been modified by this addition.

• Execute the scene.


• Pour une plus généricité du script, ajouter un attribut public dans la classeRotateCubeavant la
fonctionStart():public float speed = 3.0f;

• Modify the script accordingly.


• Youwillnoticetheappearanceofthisattributeintheinspector(3),whichallowsyoutomodifythevalueof
the rotation speed without editing the script each time. Note that the value 3.0 is the default value
when adding the script to a new object.

• Test with different values.

5. Management of interactions
Unity 3D allows capturing basic inputs such as keyboard key presses or actions of
the mouse. However, in order to allow for greater generality and not hard-code everything
Commands in scripts, Unity 3D offers an Input Manager system. For more
for information, see the documentation available at the following address:
[Link]

We now want to modify the script that makes the cubes rotate so that we can activate and
disable rotation. By default, rotation will be disabled. When the user presses the 'r' key
(and then release it), the cubes will start to spin. Then when he presses the 'r' key again
(and will then release it), the cubes will stop rotating, and so on: as soon as the 'r' key is
Supported, the cubes will change their behavior. To do this:

• Aller dans le menu (Edit > Project Setting > Input), ajouter un nouvel axe en modifiant la
size. Change its name to 'ActivateRotation' for example and set the Positive
Button like the "r" key.

• To retrieve the 'ActivateRotation' event in your script, you will be able to use the
following functions that return booleans (see documentation with Mono's auto-completion):

4
- [Link]("ActivateRotation")
- [Link]("ActivateRotation")
- [Link]("ActivateRotation")
• To verify the proper functioning of your script, you can display it in the editor's console (7)
a message each time the 'r' key is pressed. To do this, you will be able to use the syntax
next: [Link]("your message");

6. Simple mouse interaction programming


We wish to create a new script that allows pushing the cubes when we click on them and
The color of the cube changes when we hover the mouse over this cube. To do this, we will use the
functionsOnMouse…from the parent class MonoBehaviour:
- void OnMouseDown ()
- void OnMouseDrag ()
- void OnMouseEnter()
- void OnMouseExit ()
- voidOnMouseOver ()
- void OnMouseUp ()

• Create a new C# script that you will name MouseInteraction.


• Create two private attributes of type Rigidbody and Renderer in order to store links to the
RigidBody (for physics) and the Renderer of the object (to be able to access the material)
change the color).

• In the Start() function, initialize your two attributes using the functions:
GetComponent<Rigidbody>() GetComponent<Renderer>()

• To push the cube, you can apply a force on its Rigidbody using the function
AddForce(Vector3D val_dir_force). The [Link] attribute allows access to the camera.
[Link] gives the vector pointing forward of the
camera. However, it will need to be multiplied to achieve sufficient force.

• To change the color, you can access the [Link] on the Renderer and to
the [Link] on the material: [Link]. This should allow you both
to store the initial color of the object (to restore it once the mouse has left the cube) and to
change this color using a predefined color such as: [Link].

For more information on classMonoBehaviour and its functions, see the documentation:
[Link]

7. Navigation by reusing keyboard/mouse interaction scripts


Unity 3D offers several components to manage different types of basic interactions. In this part,
we will add a component to navigate in the scene like an FPS:

• Import the packageCharacters(Assets > Import Package > Characters).


• Add the prefab named FPSController to the hierarchy.
scene (2). He has his own Main Camera: disable the Main
Camera used previously by unchecking the box in the inspector (3).

• Modify the position of the FPSController to have a first-person view.


person from the stack of cubes, while being placed on the floor.

• Execute the scene, test the commands (arrows, space bar, mouse) and also listen to the sound produced.
5
8. Dynamic creation of objects
We now want to dynamically create objects in the 3D scene. To do this, Unity 3D offers
a function Instantiate(GameObject obj) that allows dynamically instantiating in the scene a
copy of the object passed as a parameter. The object passed as a parameter can be an object already present in the scene
or a prefab if one does not want the object to already be in the scene.

We want to ensure that yellow cubes are launched in the direction of the camera when
the user right-clicks the mouse button:

• Créer un nouveau script C# que vous nommerezCreateOnClick.


• Add this script to the FirstPersonCharacter (the object that follows the mouse movements) which is
the son of the FPSController. He is also the one who carries the camera.

• Create a public GameObject attribute aCopier that will allow you to store a reference on
the object to instantiate. This attribute will need to be initialized by dragging and dropping the CubePrefab into the
champs de l’attribut dans l’inspecteur (3) lorsque l’on a sélectionné leFirstPersonCharacter.

• By default in the project Inputs, the right click generates the 'Fire2' event that you can
Capture with the functions getButton…just like for the event 'ActivateRotation'.

• At each right-click:
- Instantiate a new cube with the function Instantiate(GameObject obj),
- Change its color,
- Place it in front of the FirstPersonCharacter, otherwise it will be ejected anywhere.
direction (collision with the FirstPersonCharacter)
- Apply a force to launch him forward.
Note that you can also remove objects from the scene using the function Destroy(GameObject obj).

9. Load a geometry
We want to load the 3D model of a car with textures applied on it:

• Download the archive Ford_Mondeo.zip from the course website. Unzip this archive. It contains
a .obj describing the mesh, a .mtl describing the colors and the placement of textures, as well as the
texture images .tga.

• Drag and drop the entire folder containing the various model files into
The project explorer (4). The model should be loaded in Unity 3D.

• Drag and drop the model into the scene tree (2) to add it to the scene. Adjust its size.
pour qu’il soit à la même échelle que les cubes et sa position pour qu’il se trouve près de la pile de cubes.

• The loading of new 3D models often suffers from minor compatibility issues across all
the 3D libraries. Note that here, the transparency of the car windows was not preserved. Find
the material that corresponds to the windows and change its Rendering Mode, as well as the component
alpha (A) of the color of the Mapsafin making the windows transparent.

• Try throwing cubes at the car or walking through it. What do you observe? Why?
• Find a solution to solve this problem. You will be able to use Box Colliders that you
You will place yourself or add Mesh Colliders to the appropriate parts of the car.
(cf. images on the following page). The 2nd solution is more precise, but more time-consuming to calculate.

6
10. Create an executable for the scene
Finally, we want to create an executable for this scene in order to be able to visualize the scene in full screen and
possibly distribute it:

• Go to File > Build Settings...


• Select the target PC, Mac & Linux Standalone. Note that there are other targets.
interesting such as WebGL (see the executable of the tutorial scene on the course website).

• Click on Build.
• Test your executable.
YoucanthenclickonPlayerSettings...atthebottomoftheBuildSettingswindow...
explore the different parameters of the player (dialog box at launch, resolution, etc.).

11. To go further
Explore the Assets, GameObjects, Components, and the documentation to add properties or
animations to your scene (i.e. particles, collisions...).

In particular, you can load the package vehicles, test it, and try to understand how
how the different vehicles work.

12. Contact
Cédric FLEURY - cedric.fleury@[Link]

You might also like