Flip The Bed And Roll Up The Carpet — A Weekend Exploring Unity!Molly Carlot-ClarkeBlockedUnblockFollowFollowingJan 28Coming from a visual and industrial design background I have been eager to explore how you create visuals, visualize data, virtual reality, 2D/3D games, and other experiences programmatically.
A week ago I finally took the leap and began exploring Unity for the first time since beginning my programming journey.
The task I set for myself was to build a 2D top-down game for a hackathon.
And not only did I learn how to build exactly what I had imagined, I came away with a functioning game, “Get Out Da’ House”, to show for it, and I had a lot of fun in the process!This is not a ‘how to’ tutorial on Unity, there are many resources already available.
Instead this is a peek into some of the challenges I faced and the main things I learned over a long weekend with the Unity engine, and C#.
Why Unity?Even though there are several game design options out there like Unreal, Godot, and native libraries like Three.
js, PlayCanvas and Phaser, I wanted Unity to be my first introduction into game design.
With Unity I would be able to do so with C# and because of its popularity I knew would have a lot of resources and support to help when I got stuck.
The ConceptThe object of the game is to score points by moving all the pieces of furniture out of the house before time runs out.
Each piece has a value that varies with size and weight.
With Unity I was able to set the value, the weight and approximate what the ‘drag’ would be for each piece getting moved across the room.
These elements (value, weight, drag) added increased difficulty to the central task which encourages the player to strategize which item to move first or last.
Wait, clock winding down?!.You can throw furniture out of the window, but beware you will lose points for that.
If you score enough points before time runs out, congratulations, you move onto the next level, a one bedroom apartment, and so on and so forth.
With the concept fleshed out, I went ahead and started to put my learning together step by step.
My plan was to start small, with one piece, and then scale.
If I could create one piece of furniture, give it a value, create its ‘physical’ properties, select it, move it accordingly around the floor, rotate it and get it out of the room then I would be in great shape to create the rest of the pieces.
The first programming step was to set up Unity, create a new project, make sure I selected its 2D preferences and the correct local path in my computer to keep things nicely organized.
With Unity opened, the next step was creating the scene.
The SceneOne of the first things I learned with Unity was the use of assets:Wikipedia lists the following definition: ‘Game assets are the “things” that go into a game.
Some examples of assets are artwork (including textures and 3D models), sound effects and music, text, dialogue and anything else that is presented to the user.
’In short, pretty much everything is an asset, including the scene.
Inside my Assets folder I created my Scene folder and in it, the name of my scene.
Unity has an extensive assets store with free and paid options.
But as time was of the essence, I decided it would be faster to create my own so with a little Adobe Illustrator and Photoshop I was on my way creating the first level of my game: an empty studio apartment for my background and multiple pieces of furniture, including a flat screen TV, and of course, a little potted plant — every studio needs one.
You import your assets into Unity as sprites — I imported mine as .
png files with a high screen resolution.
Once they are imported you can move them into the hierarchy to become a gameObject.
For my first gameObject I went ahead and moved my studio scene into the hierarchy as background and scaled it appropriately to fit within my game field.
When you add a sprite directly to the hierarchy it automatically creates a gameObject with Transform and SpriteRenderer components in it.
In addition to these, I also added several BoxCollider 2D to account for walls, so my pieces wouldn’t fly through the scene when moved.
Inside the studio gameObject I placed my door and window gameObjects.
I went ahead and moved the other sprites to the hierarchy as well — moving these meant adding physics in the form of Collider 2D and Rigidbody 2D components.
At this point I also learned about sorting layers where you can create and place your gameObjects in different layers according to what order you want them to render.
Note that the bottom most layer will render last which I found this particular useful for my 2D game.
Next step: game mechanics.
Moving the game objectsBefore diving right into scripting with C# and creating components (Like React! ❤), I wanted to think through my logic and investigate the overall picture of what I needed.
To move each piece of furniture or gameObject out of the house I would a) select and drag, or b) select, rotate and drag.
My first instance was the coffee table, a capsule like gameObject that needed to be rotated in order to be thrown out the door ????.
To start, I created a gameController gameObject and within it I placed my first C# script, Controller, which would handle most of the dynamics of my game, movement, speed, rotation, UI text, time and score.
Get Out Da’ House gameHowever, when everything was working well with the coffee table and I decided to add functionality to the other gameObjects, I soon realized that splitting my logic into separate components would make my game more manageable and scalable.
So I created four different components: Controller, Furniture, MouseSelect and Audio.
The Controller component included:Movement — Horizontal, vertical and rotationController component C#Adding the scoreController component C#And, setting the text for the scoreController component C#The MouseSelect component simply contained the logic of which gameObject to select using a mouse click.
MouseSelect Component C#The Furniture component included how the furniture would ‘exit’ and ‘enter’ the scene.
Entering the scene was a later consideration when thinking about how to rotate and move larger pieces out of the house.
Furniture component C#The regular sized pieces would rotate on the y-axis, but what to do for the large ones, like the bed, which will not fit through the door or even the window.
What if I could pretend rotate this 2D gameObject in its “z-axis”?.To do so I had to consider a few things.
I would create a new side view bed sprite and import it.
Default bed gameObjectAlternate side bedUp gameObjectIn my Furniture component I added public variables for a default sprite and an alternate sprite, so they would show on the editor, and I also included a function changingTheSprite.
Furniture component C#This function would allow me to switch between my default sprite (bed) and my alternate sprite (bedUp).
It worked!.I was able to switch, however, even though the size of the alternate sprite clearly fit through the door I was not able to pass it.
After inspecting it further I figured out I needed to also change the shape of its collider to match.
Once I did, success!The newly rotated bed moved out of the house, scoring the highest points in the process.
I applied the same logic and functionality to the rug.
And so all the pieces were able to be moved, rotated and thrown out the house via the door or the window.
And what is a fun game without some tunes to match?.My last component was Audio.
I imported my music as an asset into Unity and added the functionality to play the music clip on game start.
That was my weekend with Unity, I hope you have fun with yours!.