Unity 3D Basics: Creating a Simple Scene
Unity 3D Basics: Creating a Simple Scene
[Link]
Documentation : [Link]
Tutorials:[Link]
• 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).
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:
Interactions in the graphic editor can be carried out using the following buttons:
• 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.
• 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.
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");
• 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]
• 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:
• 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:
• 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]