2. Godot Fundamentals – Moving from Unity to Godot: An In-Depth Handbook to Godot for Unity Users

© Alan Thorn 2020
A. ThornMoving from Unity to Godothttps://doi.org/10.1007/978-1-4842-5908-5_2

2. Godot Fundamentals

Alan Thorn1 
(1)
High Wycombe, UK
 

So you’ve now installed Godot and Visual Studio Code. That’s great! Chapter 1 explained how to configure Godot on your computer – Windows, Mac, or Linux – and how to create Godot projects successfully. This chapter picks up from the previous to get you started quickly. Specifically, it’s a high-powered conversion course. It helps you move from the already familiar world of Unity into the new world Godot. In this chapter, you’ll learn how to create Godot projects, how to use the editor interface, how to convert from Unity’s language and terminology into Godot’s, and how to build scenes with basic game functionality. If you’ve never used Godot before, then you absolutely need to read this chapter. So let’s go.

Note

Even if you’ve used Godot before, it’s strongly recommended that you read this chapter. It covers Godot’s most core features, comparing them to Unity’s and also making important distinctions that you need to know about.

Unity comes with a whole bunch of names and words you’ll already know, such as GameObject, Component, Scene, Script, Asset, Hierarchy, Inspector, and more. Godot unsurprisingly has exactly the same concepts and ideas! But it sometimes uses different names for them. Let’s start then by exploring the names we’ll be encountering extensively throughout this chapter. Table 2-1 lists how Unity’s names and terms compare to Godot’s. Later, we’ll see more in-depth explanations for them and also make important distinctions.
Table 2-1

Terminology: Unity vs. Godot

Unity

Godot

Project

Project

Scene

Scene

GameObject

Node

Prefab

Scene

Hierarchy

Scene Tree

Project Panel

File System

Inspector

Inspector

Empty Object

Spatial Node

Asset

Resource

Tag

Group

Godot Projects

The first step in working with Godot is to create a new Project , just like working with Unity. In Godot, “One Project” refers to “One Game” or “One Simulation,” or – more generally – one complete experience. By creating a new Project, Godot effectively creates a folder in your computer’s file system, and it will contain all necessary files for your game, including metadata, textures, meshes, sounds, animations, and more. If you ever want to share your project with other team members or friends – allowing them to open it inside Godot for editing – then you’ll need to send them your complete project folder. To create a new Project, simply launch the Godot application, and you’ll be presented with the Project Creation Introduction Window. This is basically Godot’s version of the Unity Hub . From this Window, select the Projects tab and click the New Project button from the right-hand margin. See Figure 2-1.
Figure 2-1

Creating a New Godot Project

The project creation dialog is small and contains very few options. But they’re important. By default, the confirmatory Create & Edit button in the bottom-left corner will probably be disabled, requiring you to make some changes. See Figure 2-2.
Figure 2-2

The Godot Project Creation Window

Specify a project name using the Project Name text field and then click the Create Folder button next to the field if the Project Path location is suitable for you. The Project Path specifies the folder on your computer’s file system where a new sub-folder will be created, matching the Project Name. It will contain all project files and become the Project Folder. Be sure to leave the option OpenGL ES 3.0 enabled, unless you’re making a game for very old hardware, older mobile hardware, or web games. In this book, we won’t be doing that! Godot supports Windows, Mac, Linux, Android, Web, and (soon) iOS. See Figure 2-3.
Figure 2-3

Choosing a Project Folder

When you’re done, click the Create & Edit button to generate your Godot Project. When completed, Godot will open the Project inside the Editor Interface, ready for you to edit and begin development.

Note

When project creation is completed, you may see a warning window titled Important - C# Support is not feature complete. This Window outlines some limitations to the current C# language support in Godot and emphasizes that its support is still in development. You’ll probably be pleased to know that, as of Godot 3.2, C# support – while still in development – is extensive and powerful.

Editor Interface

You’ll spend most of your development time using the Godot Editor Interface, building games and other projects. The Editor Interface lets you import Assets, create Scenes, and build Projects. Its default layout looks and works similarly to the Unity engine. Let’s see a comparison. See Figures 2-4 and 2-5.
Figure 2-4

Unity Editor Interface: (1) Hierarchy Panel, (2) Scene Tab, (3) Inspector, and (4) Project Panel

Figure 2-5

Godot Editor Interface: (1) Scene Tree, (2) Scene Tab, (3) Inspector, and (4) File System

Note

Many screenshots included in this book feature the Godot interface in its light color theme, as opposed to the darker, default theme. You can choose the Interface theme by selecting EditorEditor Settings from the application menu to show the Settings dialog. From there, you should choose Interface ➤ Theme from the options list and set the Theme Preset drop-down menu to Light.

By default, Unity creates an empty scene for you in your new project. You can edit this scene through the scene tab – as you’ll already know. In contrast, Godot doesn’t create a scene automatically, even though it initially appears like a scene exists by looking at the 3D viewport. You’ll see a skyline and a 3D Grid in the viewport (somewhat counter-intuitively) – making it look like a world exists. See Figure 2-5. Despite appearances, however, Godot is different from Unity. It’s actually in a “standby” mode, waiting for you to open any existing scene (if you have one), to import a scene from a file or to create a new scene. We’ll do the latter in the next section.

Creating and Editing Scenes

Scenes are worlds or levels. A scene is a single, unified coordinate space in which objects exist in a hierarchy, like characters, buildings, and weapons. Unity calls such objects “GameObjects .” Godot calls them Nodes. Together these Nodes form a world, such as a town, a house, a space station, or any other type of location. Typically, a game consists of one or multiple Scenes. These may be presented during gameplay either successively, like when the player progress to the next level, or intermixed together at different times, such as when a town environment changes seamlessly to a forest during an RPG. In Unity, a Scene is conceived as a monotype; that is, there’s literally only one type of scene. Consequently, your development workflow in Unity will be to create an empty scene and then put things in it: 3D objects, UI elements, and also 2D elements as needed. Simple. You can – of course – make fully 2D games in Unity, as opposed to 3D. Naturally, 2D and 3D seem to be different coordinate spaces; but really – when you make a 2D game – your 2D objects will just live in a 3D world and they’ll only be presented in 2D to the camera. Godot is different, however. Unlike Unity, Godot does divide Scenes into different types, specifically three types: 3D Scenes, 2D scenes, and UI (User Interface) Scenes. A scene’s type is determined by its Root Node type , that is, by the type of Object at the top of the scene hierarchy. Let’s try a practical example to show you what I mean.

Note

I strongly believe it’s a mistake to divide scenes into different types as Godot has done. I think the Unity understanding of a Scene is simpler, more elegant, and more powerful as it separates the world space from the types of objects that inhabit it, allowing you to easily and intuitively mix different kinds of objects, like 3D objects and UI elements. I hope that Godot will revise its scene conception in future releases.

To create a 3D scene, you select SceneNew Scene from the File menu. This creates a new Scene tab in the interface, and the Scene Tree tab prompts you to generate your first, top-level Node. For a 3D scene, you must select 3D Scene. See Figure 2-6.
Figure 2-6

Creating a 3D Scene Root Node

After selecting 3D Scene from the menu, Godot auto-generates a new Node as the topmost node in the scene hierarchy (Tree). This node is called a Spatial. A Spatial Node is the Godot equivalent of a 3D Empty Game Object from Unity. It represents a location in 3D space. It can be moved, rotated, and scaled; but it has no substance or appearance, like a mesh. The Spatial node exists in 3D space. This is why any scene with a Spatial node as the Root of its Scene Tree will always be a 3D scene. See Figure 2-7.
Figure 2-7

Spatial Nodes Are at the Root of 3D Scenes

After creating any scene, you’ll need to save it. A scene with unsaved changes appears with an asterisk symbol (*) beside its name in the Editor. You can save the open scene by pressing Ctrl+S (Windows and Linux) or Cmd+S (Mac) or by choosing SceneSave Scene from the application menu. See Figure 2-8.
Figure 2-8

Saving a 3D Scene

Scenes are saved as an Asset (Resource) of the project and will appear in the File System panel. Each scene is saved in the .tscn format .

Note

The tscn format defines the contents of a scene in a text file form. Godot uses this text file to construct the contents of a saved scene when shown in the viewport or during gameplay. Being a text file, scenes can be edited and modified using a text editor. This makes it useful to batch process a scene or make far-reaching edits by using Search and Replace tools. More information can be found on the tscn format at https://docs.godotengine.org/en/3.1/development/file_formats/tscn.html.

Creating a 2D scene or a UI scene consists only on generating a different root node. For 2D scenes, choose 2D Scene; and for UI scenes, choose User Interface. Both 2D and UI scenes look substantially different in the viewport, as they use a 2D coordinate system. See Figure 2-9.
Figure 2-9

Creating a 2D Scene

Scenes and Nodes – Adding a Cube

So you can now create 3D, 2D, and UI scenes directly from the Godot interface. It’s easy. As mentioned, Godot distinguishes between scene types, between 2D and 3D coordinate systems. In this section, we’ll focus on the 3D scene for demonstration, because it’s the closest type to a Unity scene. Even so, Godot Nodes (GameObjects) work the same across scenes. So everything you’ll learn here for 3D scenes will be good for 2D and UI ones too. Let’s explore a really basic task, specifically how to generate a basic cube mesh primitive in the scene. In Unity, that’s easy to do. For example, in Unity 2019 and 2020, you just choose GameObject3D ObjectCube from the application menu. And voila, a cube is made! See Figure 2-10. In Godot, however, the situation is different. There is no Primitive Creation menu option!
Figure 2-10

Creating a Cube in Unity. Easy!

In Godot, you must make mesh primitives manually. To do this, select the scene root node by left-clicking it from the Scene Tree panel. For a 3D scene, the root node will be a Spatial Node. To create a Child Node from here, right-click the Spatial Node from the Scene Tree, and select Add Child Node from the context menu. See Figure 2-11. This displays the Node Creation Window.
Figure 2-11

Creating a New Node

From the Node Creation Window, enter MeshInstance into the Search field. This applies a filter to the Matches list, showing you all available, matching nodes that can be added to the scene. See Figure 2-12. The MeshInstance node is the Godot equivalent of a Unity GameObject featuring both a MeshRenderer and MeshFilter component. It allows you to display a mesh in a 3D scene. Select the MeshInstance node and click the Create button.
Figure 2-12

Creating a New MeshInstance Node

Note

Pretty much anything functional in a scene – lighting, navigation, post-processing, and so on – must happen through Nodes. Godot is far more node driven than Unity. Unity features many Editor Windows as tools that are used to work on GameObjects. There is a clear distinction between a GameObject in the world and a tool for editing properties of that object. Godot, by contrast, embeds much Editor functionality into a scene through nodes, as we’ll see. You create nodes by using the Node Creation Window, making this tool highly important. More information on Scenes and Nodes can be found online here: https://docs.godotengine.org/en/3.1/getting_started/step_by_step/scenes_and_nodes.html.

After adding a MeshInstance node to the scene, it’ll appear as a Child of the previously selected Node in the Scene Tree. If the MeshInstance gets created as a child of an unintended node, then you can fix this by simply dragging and dropping the MeshInstance onto another node inside the Scene Tree Panel. This creates a parent-child connection between the nodes. Dragging and dropping Node X to Node Y will always make Node X a child of Node Y. See Figure 2-13 where a MeshInstance node is a child of the Scene Root object, a Spatial Node.
Figure 2-13

Adding a MeshInstance As a Child Node of a Spatial

MeshInstance nodes are used for displaying meshes in 3D scenes. Each MeshInstance node is a single, unique instance of a mesh inside the scene. They can display complex meshes imported from third-party software like Blender (www.blender.org) or auto-generated Primitive meshes like Cubes and Spheres. To generate a Cube, select the MeshInstance by left-clicking it in the scene viewport, or from the Scene Tree Panel, and then (from the Inspector) click the Mesh drop-down field and choose New CubeMesh to create a new Cube Mesh Asset in the Project. See Figure 2-14.
Figure 2-14

Generating a New Cube Mesh Asset in the Godot Project...

After selecting New CubeMesh, a cube will be shown in the scene. By default, all meshes are visible in Godot. Clicking this option generated a new procedural mesh that Godot has saved internally as a Resource (Asset) of the Project, even though nothing extra is shown in the File System Panel. The newly generated mesh has been assigned as the Mesh property for the MeshInstance object, which positions the mesh by the Transform Component. See Figure 2-15.
Figure 2-15

Generating a Cube Mesh and Displaying It in the Scene

If you want to change the Location, Rotation, or Scale of a Node in a 3D scene, then you can use its Transform Component. To view this, select the Node by left-clicking the viewport, or select from the Scene Tree, and then expand the Transform group inside the Inspector. You can type numerical values directly into the X, Y, and Z fields to set them immediately; or you can left-click and drag your mouse over the X, Y, and Z labels beside the fields to smoothly change the values. Dragging leftward decreases the field and rightward increases. See Figure 2-16.
Figure 2-16

Adjusting Transformation Properties for a Node

Congratulations! You just made your first Godot 3D object. It’s easy and rational but significantly distinct from the Unity method. There is however an important limitation. Specifically, Godot will not automatically generate any colliders for a primitive object made in this way. Consequently, physical objects – like first-person controllers and rigid bodies – will smoothly pass through the cube mesh as though it were an apparition or hologram, an object without substance. This is not a problem here specifically; but it would be a problem if you wanted objects to bump into the cube or to be obstructed by it. We’ll see shortly how to add collision properties to the cube.

Navigation and Transformation

We saw previously how to create Godot projects. Plus, we saw how to add primitive objects – like cubes and sphere – inside 3D scenes. Although we saw important differences in the workflow between Godot and Unity, the underlying principles are really the same. You should first create a Scene and then simply put objects in it. This section looks at the main Godot control system, both for moving objects around a scene and for navigating the scene camera. You’ll be pleased to know that there’s hardly any difference between Unity and Godot here – if you set things up right.

Let’s start by configuring Godot to use the Maya Control scheme. This makes Godot’s controls behave like Unity’s controls. To do this, choose EditorEditor Settings from the application menu. This displays the Editor Settings Window. From here, select Editors ➤ 3D from the options list, and then set the Navigation Scheme drop-down to Maya. See Figure 2-17.
Figure 2-17

Configuring Godot Controls...

Note

If you’re using Godot on a laptop or mobile computer with a trackpad for a mouse, then you’ll want to enable Emulate 3 Button Mouse to get access to all viewport navigation features.

Zoom

You can zoom in and out of the viewport by rolling the middle mouse wheel forward or back, respectively, or by sliding two fingers along a trackpad. See Figure 2-18.
Figure 2-18

Viewport Zooming

Note

Technically, with Zoom, we’re actually dollying the camera and not zooming. That is, we’re moving the camera forward in 3D space, as opposed to keeping the camera static but changing the focal length of the lens.

Pan

Pan allows you to slide the viewport camera left or right or up or down. Just drag the mouse around while holding down the Alt key and middle mouse button. If you’re using a trackpad, then hold down the shift key and drag around the pad. Moving left, right, up, or down will pan the view in that direction. See Figure 2-19.
Figure 2-19

Panning Around the Viewport

Frame and Orbit

Frequently, you’ll want to rotate your viewport camera around a centered node – like a mesh – to get view of it from any angle. You can do this using the Frame and Orbit technique, which is really simple. Start by “Framing” the object you want to rotate around. To do this, select the object that will be your target – either by left-clicking it on the viewport or by clicking it from the Scene Tree Panel. To frame the selected Node, you can either press the F key on the keyboard or double-click the Node icon from the Scene Tree Panel – either way works. When you do this, the viewport will center its aim on the selected node. Then you can freely rotate (orbit) around it by holding the Alt key (Option key on a Mac) and then left-clicking and dragging the mouse in the direction of rotation. See Figure 2-20.
Figure 2-20

Orbiting the Framed Object

First-Person Controls

Now then, who can truly resist exploring their 3D worlds in first-person mode, as though you were actually there? Certainly not me! So thankfully, Godot offers first-person controls, allowing you to view the world directly from the perspective of a character in the world. To access first-person controls, hold down the right mouse button, and then use the W, A, S, and D keys on the keyboard to move the camera forward, left, down, and right, respectively. See Figure 2-21.
Figure 2-21

First-Person Controls…

Selecting, Moving, Rotating, and Scaling Nodes

You’ve seen how to move the viewport camera to get a better view of a scene. But you can also move, rotate, and scale nodes in the scene – just like Unity. The controls are the same too. To select an object without transforming it, just hit the Q key and then left-click the node in the viewport, or select it from the Scene Tree Panel. You can also activate the Select Tool from the Godot toolbar, after which you can left-click objects in the viewport to select them. See Figure 2-22.
Figure 2-22

Select Tool

You can use the W key to activate the Move tool, allowing you to move selected Nodes around using the viewport Gizmo along the X, Y, or Z axis. Similarly, you can rotate objects using the E key and scale them with the R key. After activating the Move, Rotate, or Scale tools, you should simply click and drag the selected Node in the viewport to apply the tool, just like Unity. See Figure 2-23.
Figure 2-23

Translate, Rotate, and Scale Tools for Transforming Nodes

Local and World Space Transforms

Moving objects around in 3D is easy. But, as soon as you start rotating objects and then moving them, you might run into confusing coordinate troubles. For example, if a character that’s standing still while facing an oncoming target ahead is rotated to a new orientation, where should the character go when you move them forward? They could move forward in world space – along the Z axis – which might be different from the direction in which the character is now facing after being rotated. To solve this problem, Godot understands the scene using two different coordinate systems. The first is World Space. This is an absolute and fixed coordinate system divided into X, Y, and Z. Every spatial node in the scene has a position in world space, measured from the world origin at (0,0,0). The second is Local Space. Each spatial node has its own Local Space, and each node is the origin of its own space. When a Node is rotated, the coordinate system rotates with it, so that the Z axis in local space always points forward, in the direction that the object is facing. Godot makes it easy to switch between World and Local spaces. You can do this by pressing the T key or by toggling the Local/World space icon, to switch back and forth between Local and World space, making transformations – like move, rotate, and scale – easier to apply. See Figure 2-24.
Figure 2-24

Local vs. World Coordinate Space

Scene vs. Game Mode

The Unity interface features a Scene and Game tab. These represent two distinct modes for the Unity Editor. In the Scene tab, you build a game world; add objects and configure the code. In the Game tab, by contrast, you play-test your game from the perspective of a gamer or a game tester. See Figure 2-25.
Figure 2-25

Scene and Game Mode in Unity

The great thing about Unity’s Game Mode is that, even while your game is running in the Game tab, you can still switch over to Scene mode and inspect your objects and the scene live. This makes debugging much simpler and even fun! Godot, however, doesn’t feature a Game tab exactly like Unity’s; but, you can still do most of the same things. Let’s see how. To play-test your game, simply click the Play icon on the application toolbar or press F5 on the keyboard. See Figure 2-26.
Figure 2-26

Launching Godot’s Play Mode

If this is the first time you’re pressing Play, Godot will probably display a confirmation notice, asking you to specify the Main Scene. The Main Scene is the default scene, the scene that’ll always load first when your game is executed. From the Confirmation Window, you can make the active scene the Main Scene by simply choosing Select. The active scene then becomes the Main Scene and will execute. See Figure 2-27.
Figure 2-27

Setting the Main Scene

You can always change the Main Scene by choosing ProjectProject Settings from the application menu to display the active Project settings. From here, select ApplicationRun from the Settings list, and then either remove or choose a different scene for the Main Scene field. See Figure 2-28.
Figure 2-28

Changing the Main Scene

If you just want to play the active scene immediately, as if it were the Main Scene, then just click Play Scene or press F6 on the keyboard. See Figure 2-29.
Figure 2-29

Starting to Play the Active Scene

After entering Play mode, your scene will always run in a separate, free-floating Window, which is a completely separate process. See Figure 2-30.
Figure 2-30

Playing the Active Scene

Remember, your scene will only be visible in the Application Window during Play Mode if it has a camera node in the tree. Scenes are not created with cameras by default, so you’ll probably need to add one manually. You can do that in the same way as adding any other Node. Right-click the root node in the Scene Tree panel, and then choose Add Node from the context menu. From the Node Creation Window, select Camera to add a scene camera. See Figure 2-31.
Figure 2-31

Creating a Camera Object

When your scene has a camera Node, then Play mode will display the scene from the perspective of the camera. See Figure 2-32.
Figure 2-32

Playing a Scene with a Camera Node

Since Game Mode runs in a separate Window, as a separate process, it’s completely detached from the main Godot Editor and the scene. Consequently, any changes you make in the Godot Editor during Game Mode – such as moving a mesh or moving the player character – will not be synchronized to the Game Window by default. However, you can synchronize changes between the two modes. To do this, select DebugSync Scene Changes from the Application Menu. Now, when you hit Play with this option activated, any changes you make in the scene during Game mode will be updated in the Game Window automatically and immediately. See Figure 2-33.
Figure 2-33

Activating Sync Scene Changes

Note

Unlike Unity, where any changes made to a scene during Game mode will be reversed as Game mode ends, all Godot changes will persist and remain.

Sync Scene Changes works great whenever you need edits made in the Scene view to transfer directly to the Game Window. But sometimes you need a more extensive connection than the one-way one from Editor to Game. To create a two-way connection between editor to game and game to editor, you should enable the Remote View from the Scene Tree Panel during Game Mode. This updates the Editor to reflect the Game View and updates the Game View to reflect the editor. This allows you to select objects in the Editor and to inspect their properties while the game is running, just like Visual Debugging in Unity. To do this, click the Play button to enter Game mode, and then click the Remote button from the Scene Panel. See Figure 2-34.
Figure 2-34

Activating the Remote View from the Scene Tree

Excellent! You can now use Godot’s Play Mode and Visual Debugging features. At first sight, the Play mode features seem limited; but it turns out they’re extensive and offer a lot of parity with Unity’s Game Mode. Next, and finally, we’ll turn to working with Resources in Godot.

Resources

Basically, a Resource in Godot compares to an Asset in Unity. It includes all the files on which your project depends: meshes, textures, audio, animations, scenes, and more. Some resources are fundamental insofar as their structure means they must necessarily contain no external dependencies, for example, image files, audio files, and text files. Other resources are of a higher order. These may contain dependencies, such as a mesh file that depends on a set of external images for its materials or a scene file that depends on a set of meshes because it contains instances of them. Godot doesn’t actually make any formal distinction between fundamental and higher-order assets as discussed here. But, the distinction nevertheless exists in practice because higher-order assets simply won’t function as intended whenever their dependencies are missing from the project. To import an asset into Godot, you just need to drag and drop the files or folders into the File System Panel . When you do this, Godot imports any valid and supported asset files. See Figure 2-35.
Figure 2-35

Importing Different Asset Types into a Project

Godot supports many different file formats. For image files, both JPG and PNG are supported, including transparency; and for Audio, WAV and OGG are supported. From Godot 3.2 onward, the AssImp library (http://cms.assimp.org/) has been integrated into the core code, meaning that Godot now supports an extensive range of 3D formats for meshes. This includes FBX, DAE, glTF, and more.

Note

For a complete list of supported mesh types, please see the AssImp documentation here: http://assimp.sourceforge.net/main_features_formats.html.

After importing an asset, Godot copies the imported file to a hidden folder within the project. This ensures that any changes you may make during development will not damage the original if it’s ever needed again. You can change the properties for an imported asset by selecting the asset in the File System Panel and then switching to the Import tab in the Scene Tree Panel. This displays a special Inspector that applies to Assets. See Figure 2-36.
Figure 2-36

Configuring File Import Settings

Summary

This chapter explored the fundamentals of Godot that every Unity user simply needs to know. There’s more to learn, of course. But this chapter introduced the basic building blocks for creating a project and getting started quickly for 3D scenes. 2D scenes – though distinct in their coordinate space – follow the same workflow we’ve seen here. Most 3D object types in Godot have 2D counterparts. From this point forward, each chapter will build on the knowledge contained here. The next turns to C# and to creating script files in Godot.