Thursday, August 2, 2018

Day Three: Unity Game Kits

 Well, I finally understand the concept of "asset flipping."  With Unity's 2D Game Kit, you too can make an absolutely terrible platformer in thirty minutes with no knowledge of coding.  You could probably make a quite bad one in two hours.  Compared to fast-food work, this is more profitable.  I did not realize it was this easy.

I did not learn anything from the 2D Game Kit.  The tutorial it includes only explains the interface.  It tells you, for example, how to paint platforms onto the void by clicking on the tileset tool to access pictures of platforms.  Note that this is not actually a "2D Game Kit."  It is ONLY for making terrible platformers.

Anyway, there is NO information on how to open the 2D Game Kit, so see below if you want to do it for some reason.  There is allegedly a sample game included, but I still haven't found it.  I wish I could because the screenshots show dialogue and menus, and I haven't been able to find any information about how to do anything except a free-form level yet.

What I did find that was extremely helpful is this video by Youtuber Brackeys:





LINK

With this, I was able to start making some controls to move my cube.  The first thing I did was to make a "Physic Material," which, strangely, is not misspelled.  This is applied to the floor so that you can turn the friction off.  The cube can then move smoothly over the floor.


Here is where the video leads:


The important part of this is the "FixedUpdate" function at the bottom, which happens a fixed number of times every second.  First, a force is being added to the cube every time the function is called.  The effect of this is to make the cube go forward faster and faster.  The "If" statements conditionally add a sideways force to the cube, if the player is pressing the "a" or "d" cubes.  (These are the traditional left and right keys for PC players, not the arrow keys for some reason.)

This is the code you will get from following the video, but Brackeys leaves you with homework.  There are two problems with this code.  The first one is huge, that it hard-codes many values, including the movement keys.  If this code were in your game, there would be no way for the player to choose an alternate control scheme.  If you wanted to add that functionality, you would have to spend hours checking every line of code and replacing all the "d" and "a" references.  Here is how I fixed this, which I cannot guarantee is correct:


I just added some variables to contain the desired values.  I added variables for the key bindings and for the values of the forces involved.  (The values are numbers, but they have an "f" at the end to specify they are floating point decimals, even though the values listed are integers.  Apparently, if the "f" is not there, this can cause errors sometimes.  Weird.)  Ideally, I would not specify that kbMoveRight IS definitely "KeyCode.D" here in the script but fetch that information from a player preferences file.  I don't have one of those yet, though.

The more subtle problem with the code, according to Brackeys, is that FixedUpdate is not constantly being called, so if the player is taping keys quickly, you might miss some of their key taps.  Here is what I did to try to fix that:



The function "Update" is called very fast, so I put some boolean flags in there which are set if the player is tapping a key.  Later, when fixedUpdate is called, it responds to those taps.  I think this is what Brackeys suggested, but he was talking very fast in that part of the video, so, again, I don't guarantee that this is completely correct code.

To sum up, I now have a featureless white floor on which there is a red cube which quickly speeds into the distance.  If you are quick, you can move it left or right before it falls into the void.  Nice.




 ...
 
How to open (in Windows) the 2D Game Kit from the Unity Store*
*Guaranteed to work until it doesn't.

1.  Open Unity, and start a new 2D project by clicking "new" and typing in a name.  Be sure to select 2D from the Template drop-down.

2.  Close the project and Unity.

3.  With a browser, go to the Unity Asset Store, go to the 2D Game Kit page, and "Add to My Assets" the 2D Game Kit.

4.  The button will change to "Open in Unity."  Click on that.  Windows will ask, so tell it to "Open With" Unity.

5.  The main Unity menu will open, but the asset will not open.  Open your previously saved project.  The project will now have a Window in the center of the screen that has the logo for the 2D Game Kit.  Click the "Update" or "Import" button on this window.  The button will change to a slowly-incrementing counter.  When this completes, Unity will ask if you want to Import a Complete Project.  Click yes and wait some more.  A box with a progress bar will pop up.

6.  Eventually, a menu with a file hierarchy will pop up.  Make sure every file is checked, and click "Import."  Watch the progress bar some more.  For me, this took > 15 minutes.

7.  Once that finishes, the menu bar will not yet contain the extras needed by the tutorial.  Close Unity.

8.  Reopen Unity.  Reopen the project.  There will still be nothing there, but now you can go to the Unity Learn page and follow the tutorial.

No comments:

Post a Comment

Shuffling Cards When You Are a Computer

The last time I programmed anything, programs started at the top of the Main function and proceeded to the bottom...if the Unix terminal was...