Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon

29. June 2017


In this section of our Bowling with Game Engines series, we will be implementing our bowling game in Unity. The idea is straight forward, implement the same simple 3D game across a number of game engines.  The Unity game engine is a logical place to start, as it is perhaps the most popular game engine in use today.  We will go step by step through the process of creating our game, both in text as well as a video version available here.   I am not a regular Unity user, so please do keep in mind, a lot of what I am about to show may not be best practice!  All of the assets used in this tutorial are available for Patreons as part of the bowling game kit, along with project files and this document in PDF form.  Don’t worry, these aren’t needed to follow along.

Let’s jump in.  Fire up Unity and create a new project.  In this example I am using Unity 5.6.1, however any recent version should work.


First, let’s start by dragging all of our required assets into Unity.  Simply select all of the game assets you will be using (FBX models, textures, audio files) and drag and drop them from Explorer or Finder into the assets folder.  If you are using the Patreon assets, simply copy the contents of Raw Assets in the Unity project folder.

clip_image002[4]


Unity does an extremely good job of importing assets and you should require no additional steps.  We now have all of the assets we are going to need to create our game, let’s get started.




Creating the Title Screen


First, we are going to start by creating a simple title screen with some awful looping background music.  We will be using our default scene for the splash screen and later we will create another scene for our actual gameplay.  We start off by saving our untitled scene.  Simply select File->Save Scene As…

clip_image004[4]

I saved it as TitleScreen. You should now have a new item in your assets list:

clip_image006[4]


We now have a few setup tasks to take.  We are going to be showing our title image and this requires us to alter the Main Camera entity. In Hierarchy panel select Main Camera, then in Inspector we change Clear Flags to Don’t Clear.

clip_image008[4]

This causes our camera to no longer have perspective ( things aren’t drawn smaller the farther away they get from the camera ).  Generally an Orthographic camera is what you use when working in 2D in a 3D world. Setting Clear Flags to don’t clear simply causes the default skybox to not be drawn, we could have optionally defined a background clear colour if we preferred. 


Now in Hierarchy panel, click the Create button, select UI->Canvas.  Next, with the newly created Canvas selected, click Create again and this time select UI->Panel.  At this point we should have this hierarchy.

clip_image010[4]


Next we have to make a slight modification to our imported Titlescreen image.  Select the Tilescreen.png image file in the assets view, then in Inspector change Texture Type to Sprite (2D and UI).  Then scroll down in the Inspector and click the Apply button.

clip_image012[4] 


Next select Panel in the Hierarchy view, and drag our newly created Sprite over to the Source Image section in the Image (Script) section.

clip_image014[4]


If you press Play now, you should see your Title screen in the viewport.

clip_image016[4]


Now let’s add a looping music file to our title screen.  In the Hierarchy view, select the Canvas entity.  Then in inspector scroll down and click Add Component, then Audio->Audio Source.

clip_image018[4]


In the newly created Audio Source in Inspector, drag our imported audio file over to the audio clip section.  Down below, make sure Play on Awake is set and then tick the box next to Loop.

clip_image020[4]


Ok, we now have a title screen and a sound track playing!  Good work so far… now let’s add a simple script that changes scenes on click. Let’s create our script.  Right click in the Assets area, select Create-> C# Script.

clip_image022[4]


Name it PanelScript.cs.

clip_image024[4]


Double click the newly created script file and it will open in your editor of choice.  Now enter the following code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PanelScript : MonoBehaviour {

   // Use this for initialization
   void Start () {
      
   }
   
   // Update is called once per frame
   void Update () {
        if (Input.GetMouseButtonUp(0))
            SceneManager.LoadScene("GameScene");
    }
}


Here we are simply checking every single pass through the game loop if the Left (0th) mouse button (or touch) is clicked, and if it is, we load the scene named “GameScene”.


Hmm… guess we should make a scene called GameScene now shouldn’t we?  Once again right click the Assets panel, then select Create->Scene.

clip_image026[4]


Rename the newly created Scene to GameScene.

clip_image028[4]


We are now done with our title screen scene.  Make sure you save.  Before we open our newly created GameScene, we have one last step to perform.  In the File menu, select Build Settings…, then select Add Open Scenes.

clip_image030[4]


Now double click our newly created GameScene to open it up.  If prompted to save, do so.  Now it’s time to create our game!


Click here to continue to part two!

Programming , , ,

29. June 2017

Click here for Part One

Creating the Game Scene


Now it’s time to get to work on creating the game itself.  Start off by dragging BowlingLane into the scene.

clip_image002


Now move the lane so it’s at position (0,0,0) in the Inspector under the Transform component.

clip_image004


One thing I noticed is the Normal map is way too strong.  Select the Bowling Lane material (either in the Materials panel, or drill down to it).  Dial back the Normal Map strength from 1.0 to 0.2.

clip_image006


We are almost done with our bowling lane.  The final thing we need to do is add a Collision Shape to it, so the rest of the stuff in our game world will interact with it properly.  With the BowlingLane selected, click Add Component->Physics->Box Collider

clip_image008


The nice thing with Unity is, it will automatically shrink wrap the collider geometry to match your mesh, so you should have to do no additional work!

clip_image010


So, that’s it for the lane, now time to create the Bowling pins.  Simply drag the Bowling Pin into the scene:

clip_image012


Hmmmm… pretty dark. What’s going on here?  Well, right now we are getting our lighting from the Directional Light in our scene.

clip_image014


We don’t want to mess around with advanced lighting, so instead right click and get rid of it. Now lets set up Ambient lighting instead.  Select Window->Lighting->Settings

clip_image016


Change Environment Lighting->Light Source to Color, then select the Ambient Color as white:

clip_image018


Now our scene will have universal consistent lighting:

clip_image020


Now it’s time to add some physics to our pin.  Once again we have to make a collision object component, but we also need to create a RigidBody component.  Add a Box collider just like before.  Then add another Component and select Physics->Rigidbody

clip_image022

The default settings are about right, so there is nothing else we need to do here.


The final thing we need to do with our pin is apply a tag to it so we can locate it using code later. With the Pin selected, in the Inspector at the top drop down the Tag drop down and select Add Tag…

clip_image024


Click the + Icon, then in the resulting text box enter “Pin” then Save.

clip_image026


Now select the Pin again, and in the Tags list, select Pin which should now be an option.

clip_image028


Now we simply need to create 4 ( or 9… depending your bowling preferences ) clones of our pin. Select the BowlingPin in the scene graph, right click and select Duplicate.  This will create a copy with all of the settings and components defined. 

clip_image030


After duplicating, position the pin and repeat the process until you have enough pins.

clip_image032


Click here to continue to part three.

Programming , , ,

29. June 2017


Click here to go back to Part Two.


Next, we need to create a bowling ball.  Instead of importing a 3D mesh, we are going to create one.  Select Game Object->3D Object->Sphere.

clip_image002


Once created, rename it to BowlingBall.

clip_image004

While we are here, create a new Tag called Ball and assign it to our object.


Let’s create a material for our Bowling ball.  Right click assets panel, select Create->Material.  Rename the newly created material BowlingBall.  Pick the color of your bowling ball with the box to the right of Albedo.  For shine add a small amount of Metallic as well.

clip_image006

Now simply drag the newly created material over to our Bowling Ball in the scene.


We also need to attach physics to our bowling ball.  This time it’s using a Sphere Collider.  This is also a rigid body, but this time we increase the mass to 3.

clip_image008


We are going to be putting all of the logic in our bowling ball.  One thing we are going to need to do is play audio when our ball hit’s a pin. We will add this logic later, but the audio source needs to be somewhere, so why not the bowling ball. We actually covered this process earlier when we added audio to our title screen.  Simply add an Audio Source component to the ball then attach the Hit.wav file, like so:

clip_image010

Tick off the Play on Awake checkbox.


Now its time to add some scripting to our game to control input and to deal with physics collisions when they happen. Right click the Assets panel and create a new script like we did earlier. Rename it BowlingBall.cs and drag it onto our BowlingBall object.  Now double click our script and enter the following code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BowlingBall : MonoBehaviour
{
    public float force;
    // Use this for initialization
    private List<Vector3> pinPositions;
    private List<Quaternion> pinRotations;
    private Vector3 ballPosition;
    void Start()
    {
        var pins = GameObject.FindGameObjectsWithTag("Pin");
        pinPositions = new List<Vector3>();
        pinRotations = new List<Quaternion>();
        foreach (var pin in pins)
        {
            pinPositions.Add(pin.transform.position);
            pinRotations.Add(pin.transform.rotation);
        }

        ballPosition = GameObject.FindGameObjectWithTag("Ball").transform.position;
    }


    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Space))
            GetComponent<Rigidbody>().AddForce(new Vector3(0, 0, force));
        if (Input.GetKeyUp(KeyCode.LeftArrow))
            GetComponent<Rigidbody>().AddForce(new Vector3(1, 0, 0), ForceMode.Impulse);
        if (Input.GetKeyUp(KeyCode.RightArrow))
            GetComponent<Rigidbody>().AddForce(new Vector3(-1, 0, 0), ForceMode.Impulse);
        if (Input.GetKeyUp(KeyCode.R))
        {
            var pins = GameObject.FindGameObjectsWithTag("Pin");

            for (int i = 0; i < pins.Length; i++)
            {
                //collision.gameObject.transform.parent.gameObject.tag
                var pinPhysics = pins[i].GetComponent<Rigidbody>();
                pinPhysics.velocity = Vector3.zero;
                pinPhysics.position = pinPositions[i];
                pinPhysics.rotation = pinRotations[i];
                pinPhysics.velocity = Vector3.zero;
                pinPhysics.angularVelocity = Vector3.zero;

                var ball = GameObject.FindGameObjectWithTag("Ball");
                ball.transform.position = ballPosition;
                ball.GetComponent<Rigidbody>().velocity = Vector3.zero;
                ball.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
            }
        }
        if (Input.GetKeyUp(KeyCode.B))
        {
            var ball = GameObject.FindGameObjectWithTag("Ball");
            ball.GetComponent<Rigidbody>().velocity = Vector3.zero;
            ball.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
            ball.transform.position = ballPosition;
        }
        if (Input.GetKeyUp(KeyCode.Escape))
        {
            Application.Quit();
        }
    }
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Pin")
            GetComponent<AudioSource>().Play();
    }}

We publicly expose the Force value, which is the amount of energy to apply along the Z axis when we throw the ball.  Since it is public, it’s settable in the editor as a property in the script component.

clip_image012


And done!


The Video

Programming , , ,

26. June 2017


The Esenthel Game Engine has been under development for more than a decade and yet I had never actually tried it before now.  They recently released a Fantasy Tech Demo illustrating recent functionality added to the engine.  After trying out the demo, I decided to give Esenthel a shot.  The following video is a quick hands-on with the Esenthel Game Engine. Esenthel is a C++ powered/scripted game engine with a full editing environment as well as Visual Studio integration.


The Essenthel Engine has a free trial available for download and is available for Windows, Mac and possibly Linux.  The full version is a commercial subscription for 11.40$ a month or 114$ annually.  A source license costs double that amount.

Programming ,

6. June 2017


There are several game engines out there that present a code free option such as Stencyl and Construct.  Today we are looking at an open source alternative, GDevelop.  It is a cross platform, open source 2D game engine with a visual programming interface that requires no previous experience.  However there is also the ability to extend the engine using the C++ programming language if desired.  If you are new to the Closer Look series, it is a combination of game engine review and getting started tutorial helping you decide if a game engine is right for you.


As always, there is a video version of this tutorial available here and embedded below.


Without further ado, let’s jump into GDevelop.


Meet GDevelop

As mentioned above, GDevelop is an open source 2D game engine.  It is released under the MIT license for the core engine, while the editor is available under the GPL v3 open source license.  The code is available on Github.

Of course you don’t ever have to touch the source code to work with GDevelop.  You can download binaries for Windows, Mac, Linux as well as Browsers, iOS and Android in the form of GDevApp.  I will not be covering GDevApp today however.  GDevelop is able to compile native applications, HTML5 pages as well as Android applications, which is currently an experimental feature.

The vast majority of your time is going to be spent in the GDevelop editor, shown below.

image

The center of the screen is currently showing the Scene editor.  There is where you can compose scenes.  It is a tabbed view that can contain multiple open scenes as well as Events, the programming model of GDevelop, which we will discuss more shortly.

On the right hand side is the Objects Editor, which contain the building blocks of your game.  On the left hand side is the Project Manager containing the assets and scenes that make up your game.


Let’s walk through creating a sprite in a game.  In the Objects editor, right click objects and select Add an object.

image

This will show us a list of possible objects:

image


For every object except the Sprite you need to enable it before you can use it (thus why they are grayed out).  It’s as simple as double clicking a grayed out object to enable it however, like so:

image


In this case however we are going to use the Sprite object, which is already enabled.  Simply double click Sprite.  Double clicking the newly created object in the Object editor will open up the sprite editor screen:

image


At the top right corner of the editor window you will see the Images bank’s editor, click the plus icon and add the images you will use for your sprite.  Next drag the selected images down to the images section, like so:

GIF


You can rename and configure the animation (if any) under the Animations panel.

image


Now you can create an instance of your Sprite object by simply dragging it into the scene.

GIF2


As we saw earlier, there are several built in objects in addition to the Sprite object, including Admob integration, a Text object, a tiled sprite (spritesheet), etc.   Next lets move on to adding some logic to our newly created object.


Scripting in GDevelop

Logic in GDevelop are implemented using Events.  Let’s look at an example of moving the sprite around when the mouse moves.  Click on the Events tab.

image


In the Events ribbon, click Add an Event:

image


In this case we have no conditions, we want this to happen every single frame.  Hover over No actions, then select Add an Action.

image


This is where the building block aspect of GDevelop programming comes into play.  Select All Objects->Position->Position of an object.

image


Now we can set the parameters for positioning our object.  First select the object to modify, then set each parameter.  In this case we set the X and Y values to those of the mouse cursor, like so:

GIF3


Upon completion, you will see we now have an event defined:

image


In this particular case we have no condition, so our event will fire every pass through the event loop.  We could however have set a condition which causes our event to fire or not.  Here for example is a condition that will start playing some music once the scene is loaded.

image


Beyond conditions, there are other control structures you can add to events:

image


Link enables you to break link to another event sheet, enabling you to modularize your code.  It is also possible to define variables, both globally, to the scene and at the object level.  For example, right click the Project and select Modify Global Variables.

image


This now enables us to define new variables:

image


This can also be done at the object level, right click an object and select Other Properties.  Then in the resulting panel select Click to Edit...

image


While we are here, notice the Behaviors option?  This enables us to add new functionality to game objects.  Click the Add... button:

image


You will notice once again, by default all behaviors are disabled.  Double click a behavior to add it in.  Let’s go ahead and add Top-down movement as an example.  You can now edit properties of the new behavior in the same dialog:

image


This will instantly add arrow key navigation abilities to your object.  When you play your game, arrow keys will cause your object to move around screen.  Speaking of playing your game, hit the Preview button in the Scene ribbon to launch your game in your browser.

image


You can control application level settings of your project by right clicking your project and selecting Edit the property of the game.

image


This will bring up the next dialog.

image


Finally, if your project has Native extensions enabled, under the File menu you have the option to build a native version of your application.

image


Then simply click the Compile button.

image



Documentation and Community

GDevelop is reasonably well documented, with a Getting Started tutorial, several other tutorials and a decent number of starter templates to choose from.

image 


There is also a manual available online.  If you are intending to extend GDevelop using C++ however, the documentation is almost non-existent.  However being an open source project, all of the code is available.  All of the behaviors and objects we used in this example are available in source form on Github in the extensions folder:

image


GDevelop has a dedicated forum available here.  It is reasonably active with a decent sized community.  Forums are available in both English and French.


Conclusion

If open source, free and a visual programming interface are important to you, GDevelop is definitely an engine to consider.  The documentation is adequate, the engine is mostly feature complete, although annoyingly some features such as Tiled support are only available for native targets.  The entire thing is designed around extensibility and if you are willing to dive into C++, the sky’s the limit on what you can do.  My biggest complaint is a lack of polish on the UI layer, experiencing a few crashes, some UI glitches that went away on a reload and some buttons that simply do nothing.  Most annoyingly, the engine is basically unusable on a high DPI display.

It is however an easy engine to jump into and use if you are willing to deal with some UI warts.  An MIT license around the core engine is always an excellent feature.


The Video

Programming , , , ,

Month List

Popular Comments