Tip:
Highlight text to annotate it
X
Hello everybody, here we are at the tenth and last episode, regarding this introductive mini course
on Unity 3D fundamentals; as I will say in the third part of the tutorial,
it isnít at all my last video tutorial on Unity, on the contrary I have already in the works two more miniseriesÖ
anyway I will explain you everything further in the tutorial (also because your feedback
is required in certain points), at the moment, letís concentrate on the topics of this episode instead:
rotate objects, the inherent FPS Controller and the functions for changing Scenes,
useful for passing from one game level to another.
We begin from the second topic because, actually, it is the simplest one:
the inherent FPS Controller of Unity 3D. As the name suggests, itís about a Component
which incorporates the userís input management in reference to the FPSís typical ìpatternî;
actually its true name is, generally, Character Controller, given that you can use it to guide anything,
from a soccer player to a cube, like in this example.
Usually, the Controller is added in the creation phase of a new project: by doing File - New Project,
on the pop up screen, besides the field for entering the projectís name, we will find a list
of additional components, that we can select so to immediately include them in our project.
The first element of the list is actually Character Controller, therefore if we want to insert this package
in our project from the start, we select the appropriate box and we create the projectÖ
I am NOT selecting it, so to let you see how to add this controller afterwards, in an already created project:
you just select, from the Assets menu, the Import Package section and from there, needless to say,
Character ControllerÖ ok now we introduce this Controller into the scene!
As soon as the packageís import is done, just like with Terrain, which we have seen in the third episode
of this mini course, in Project we will have a folder with elements and, most importantly, new items
in the Component menu: the character section and three menu items under this subsection.
With the object that we want to move selected (in my case, the Cube placed on the platform)
we specifically choose, FPS Controller: essentially, as it happens with the other components,
like Rigid Bodies or Scripts, we are providing the object with an FPS Controller component.
Unity will inquire if we want to replace the cubeís Rigid Body Component, there by default,
with the FPS Controller, so the two elements are exclusive; we confirm our selection.
What is the meaning of all these, in practical terms? We send in execution, clicking on Play, and then
we try pressing the arrow keys, WASD or the space bar: the cube, equipped with FPS Controller,
is moving and ìjumpsî, among others with a jump that happens only when is effectively in contact
with the ground, which is something not to underestimate!
At this point you may ask me: but if Unity give us this controller as standard, why did we learn
how to move and rotate the elements through code?
The answer is simple: so you understand how it works and for showing you how the components
and the modules are arranged in Unity, this way it will be much easier for you to understand
what a Package is it made from, analyze its various Components and modify them as desired.
Done with this introduction during which we ìgot our hands dirtyî with elements of low level
(in terms of computer science, not of difficulty), we are moving on, to an inherent Controller because
is already there, thus if your purpose is to move a character with the arrow keys or ñ as we will see very shortly ñ
rotate it through the mouse, use directly the FPS Controller and modify it eventually
(and it is at this point that you will need your skills in scripting learned in the previous episodes!
You will also need them so to modify scripts of others!), to use it and modify it instead of writing code
from scratchÖ is unnecessary to reinvent the wheel, but it is also important to understand the functioning,
the elements and the characteristics of the wheel!
So, letís talk about the elements of the FPS Controller Component: by selecting the Cube, in Inspector,
we notice the presence of some new Components, like Character Controller and Character Motor Script,
each one with the corresponded items, most of which with intuitive meaning, whereby I wonít dwell
to describe them but I am inviting you to directly experiment with, I would rather prefer to focus
on the Mouse Look topic, or what to do to rotate, to change the objectís orientation, using the mouse.
In fact, right now, the cube is moving according to the arrow keys or WASD and the space bar pattern,
but it isnít rotating using the mouse, a typical characteristic of the FPS.
Looking at the FPS Input Controller script component, in Inspector, we notice that there is a circlet
next to the scriptís name; if we click, a pop up window appears from which we can choose another script,
among a set of the Character Controller package scripts, in place of the FPSÖ
ìin place ofî because the selection of a different script will replace FPS, itís possible to make a multiple selection.
In any case, we select Mouse Look and we start the game, to give it a try: actually now with
the mouse we can rotate the element around the two axes ñ the mouse directions,
however we have lost the keyboard controls, for movementÖ we return in Edit mode.
First, a note about orientation: as we have seen, the object is oriented in the two mouse directions,
but sometimes is preferable a rotation around a single axis, not the horizontal one; to change this feature,
we expand the MouseLook script and we change, always through Inspector, the ìAxesî item
from the default ìMouseXandYî, to ìMouseXî, then we try again.
Using the other items you may change other features, but they are quite intuitive,
therefore letís not waste too much time and instead go back to the discussion about the ìexclusiveî
scripts, so to regain the movement through keyboard.
Letís ponder: what is the FPS Controller script? It is, like MouseLook, a script, therefore a ComponentÖ
So, from the Component menu, we select the Script section and, here, Third Person Controller,
a set of scripts imported through the Package Character Controller.
In Inspector, a new Component of type Script will appear for the cube: Third Person Controller;
note that MouseLook is still there, so the two Components arenít exclusive, but they can coexist.
This script is already fine for movement through keyboard (try and see), but if you want you may
click upon the circlet and choose FPS Controller from the menu that will appear, so to use a proper FPS controller.
A side note (but not too much): in the scene there is a Plane that acts as a floor because
the FPS Controller replaces, as mentioned, the default Rigid Body, therefore implementing a physics simulation
as well, with gravity and other elements that we have discussed in previous tutorials,
so without Plane-floor the cube with FPS Controller would have fallen into the void.
Ok, we have seen (although in a very superficial approach, at introduction level) the FPS Controller
and the MouseLook, for rotating the elements using the mouseÖ now maybe you should pause
for a couple of minutes before we move on to the second part of the tutorial, actually much lighter
than this one: how to change game level in Unity 3D.
Here we are at the second part of this video tutorial: after we have seen how to move and rotate the objects
by adding a simple component inherent in Unity, we goÖ to another level, literally!
As we have said in the seventh episode, the environments or game levels in Unity are ìScenesî.
Furthermore we have said that, before we do the Build, the compiling of a project, we need to be sure
that we have all the Scenes included in the Build Settings panel, with special regard for the first scene
of the list, which will be the environment or screen of gameís opening.
As for the actual switching to another Scene, therefore to another environment or level,
that occurs by using a simple instruction of code, while the data transition could be done, for example,
through the PlayerPrefs, discussed previously as wellÖ as you can see, many little elements,
discussed ìseparatelyî in different episodes, come in handy now so to create something new!
Ok, letís proceed in order: currently, in Project I have only one Scene, the one that you can see,
for which, first we add another Scene with File - New Scene (Unity will ask you to save the changes
made to the first Scene; we click yes and move on).
The new Scene is completely empty, with the exception of the Main Camera.
NOTE: we save immediately this new scene with File - Save Scene, naming it for example Scene02;
there is the risk of confusion about which one is the current sceneÖ afterwards, we can switch in Edit phase,
from one scene to another by clicking on the corresponding element in Project;
for projects with many scenes, is convenient to make a folder ad hoc in Project, so to regroup them.
Then we return to the Scene01, the original one, by clicking on that element in Project.
We select the camera and add a new JavaScript script, naming it for example ChangeSceneÖ
the script I will show you is very simple, as it changes scene based on pressing the keys 1 (Scene01)
and 2 (Scene02), but obviously you can make it react to different events, for example could change scene
if detects that a character is in a certain environment (by controlling the Transform XYZ coordinates)
and presses an appropriate key, so to simulate for example a teleport cabin or the end of a level,
right to make a couple of examples.
Inside the script, in the Update section we write the following lines of code:
if(Input.GetKey("1")) Application.LoadLevel("Scene01");
if(Input.GetKey("2")) Application.LoadLevel("Scene02");
We save the script, return in game and we link this script to the Main CameraÖ attention, though!
This Main Camera is NOT the same object in both scenes!!
Therefore we have: to link the script to the Main Camera in Scene01, save, open Scene02,
link the script to the Main Camera of that scene and save this scene as well!
Make sure that you have a script of scene change to all the scenes of your project or, about the exit,
some control and closing instruction for the application, so to not being ìstuckî in some place during the execution!
We execute and press 1 or 2 to switch from one scene to the other: as you can see,
the only instruction of code that we need to change scene is ìApplication.LoadLevelî
with the name of the scene to be loaded as argument! Easy!
Well, one momentÖ sending in execution and pressing 2, Unity will inform us that canít do it because
the Scene is not in the list of the gameís scenes for the final release.
If you remember, this list is the one currently in Build Settings, discussed in the seventh episode
of this basic video course, so we do like that: in Scene01, we select File - Build Settings and,
in the window that will appear, we click on Add Current to add Scene01 in the list of the projectís scenes;
at this point, we open Scene02 with a double click on that item in Project, then again in Build Settings
we click on Add Current to add it in the final project.
Then, we can go back to the main screen of the program and execute it with Play,
so to try level changing by pressing 1 and 2.
I remind you that the Main Camera of the second level must be equipped with the ChangeScene script as well.
A potential problem is the data transit from one Scene to the other; for example, the player could
set up the name and the controls in options screen, then switch to an actual game level and, from there,
to the high scores screenÖ here, a method for transferring all these data is to convert them in name-value pairs
and use the PlayerPrefs, as we have done in the seventh episode of this video course,
as a matter of fact the PlayerPrefs are global, of the project, independently of the Scene in use,
therefore you can save a data with a SET in Scene01 and retrieve it, through the variableís name,
with a GET in the Scene02, and so onÖ so this feature is also very easy to handleÖ
provided obviously that you comply with the PlayerPrefs names from the beginning, especially if you work in group!!
Ok, regarding this lesson and this video course about Unity's foundamentals, that's all!
Thank you for listening, see you soon!