Making Things Move

Making Things Move

This is an excerpt chapter from my Weekend Code Project: Unity’s New 2D Workflow book. I hope you enjoy this free content on how to use Unity’s new 2D features. If you like what you see, please pick up a copy of the book to get access to the artwork, source code and a PDF/ePub version of the book. Working with Box2D With the addition of the new 2D workflow in Unity, there is now Box2D to handle all of your in-game physics. This is great news because you can take advantage of a common 2D physics engine, as well as avoiding the overhead of the built-in 3D physics engine. To get started, let’s add a new component onto our Player prefab. Select the Player in the Prefab folder, not the one in the Hierarchy tab, and in the Inspector panel click Add Component. From there, select Physics 2D to see all of the options. To get started, we are going to add Rigidbody 2D to our Player prefab. By adding a Rigidboxy 2D component we are telling our GameObject that it needs to obey the laws of physics in our game. We will have the ability to manage the GameObject’s Mass, Drag, Gravity Scale and a few other important properties that help make up the physics of the Box2D engine. Now that you have the Rigidbody 2D added to our prefab, you should see these new properties. Try to run the game. At this point we would expect the player to fall since it has a Gravity Scale of 1 but it just floats in one place. To...
The Camera

The Camera

This is an excerpt chapter from my Weekend Code Project: Unity’s New 2D Workflow book. I hope you enjoy this free content on how to use Unity’s new 2D features. If you like what you see, please pick up a copy of the book to get access to the artwork, source code and a PDF/ePub version of the book. Modifying the Camera At this point, we have all the basics in place to run our game. The next thing we are going to want to do is make the camera follow the Player GameObject. To begin, select the Camera GameObject from the Hierarchy panel in the Scene window. We’ll want to start by changing the Background color to black. We will also want to tweak the Size of the Camera. By default, it was set to a Size of 5. Change that to 2 and you should see the camera is now pulled in closer to the action. If you run the game, the camera is probably not going to capture the action very well. Let’s look into making a very basic camera follow script. On the Main Camera GameObject, add a new script and call it CameraFollow. In this script we are going to build a very simple update loop that will focus the camera on a GameObject we set as the target. Add the following two properties to the script: public GameObject target; private Transform targetPos;public GameObject target; private Transform targetPos; This will help us store the target GameObject and a reference to its Transform. Now, in the Start method, add the following: targetPos=target.transform;targetPos=target.transform; Next we will...
Spawning GameObjects

Spawning GameObjects

This is an excerpt chapter from my Weekend Code Project: Unity’s New 2D Workflow book. I hope you enjoy this free content on how to use Unity’s new 2D features. If you like what you see, please pick up a copy of the book to get access to the artwork, source code and a PDF/ePub version of the book. Creating a Gizmo Our game isn’t much fun or challenging right now. We need some more bad guys on the screen. Up until this point, we added new GameObjects manually to the scene. Now it’s time for us to actually look at how to dynamically create new GameObjects at runtime. To get started, we need to build a spawner, which is an object that creates other GameObjects. Go to the GameObject menu at the top of the screen and select Create Empty. This is going to be a simple GameObject that lets us add the code we need to turn it into a spawner. Name the empty GameObject Spawner and create a new script on it called Gizmo. A Gizmo in Unity is used for visually debugging GameObjects in your game. A Gizmo has its own draw method that gets called when viewing in the Scene Editor but not during runtime in your game. This will allow us to create a simple box to represent our spawner as we work with it in the Scene Editor, and it will remain invisible when the game runs. Make sure you import generic collections at the top of the class: using System.Collections.Generic;using System.Collections.Generic; Then add the following properties to the Gizmo script: public...
Managing Scenes

Managing Scenes

This is an excerpt chapter from my Weekend Code Project: Unity’s New 2D Workflow book. I hope you enjoy this free content on how to use Unity’s new 2D features. If you like what you see, please pick up a copy of the book to get access to the artwork, source code and a PDF/ePub version of the book. Working With Scenes Scenes are a critical part of your game. Not only do they represent the levels of your game but they can also be used for splash screens, game over screens as well as places a convenient workspace while you prepare GameObjects and Animations for your game. In this section we will focus on creating some new scenes for our game and switching between them. Creating A Splash Screen In this section we will cover one of the most important parts of your game, managing scenes. Unity games are broken up into individual scenes. This entire time we have been working in our main Game scene but now it’s time to create a splash screen and a game over one as well then connect them up to the game. To get started make sure you save the current scene we are working in the go to he File menu and select New Scene. We are going to call is Splash and save it to our Scenes folder. Now drag the following sprite textures into the scene: splash-background splash-monster splash-start-text splash-title splash-title-mask From here we are going to want to change the coordinates the sprites in the scene by modifying their X, Y and Order in Layer values to...
Collision Detection

Collision Detection

This is an excerpt chapter from my Weekend Code Project: Unity’s New 2D Workflow book. I hope you enjoy this free content on how to use Unity’s new 2D features. If you like what you see, please pick up a copy of the book to get access to the artwork, source code and a PDF/ePub version of the book. The Layer Collision Matrix When we talk about collision detection, we really tend to focus on two types: making things solid and detecting when two objects collide. We have already solved the first problem thanks to Unity’s built-in support for the Box2D physics engine. As far as we are concerned, everything in the game now knows how to collide with each other. The Player doesn’t fall through the floor, and it can collide with the Knights. The big issue we have now is being able to block the Player from moving off the screen, and what to do when he collides with a bad guy. To get started, let’s solve the bad guy issue first, because at this stage of the game, we can’t really move around with all the Knights spawning everywhere. If you collide into too many Knights, you’ll see they kind of bunch up and form a line. This happens because they are all colliding with each other and pushing the other ones back. We really don’t want this to happen; Knights shouldn’t collide with each other. We can quickly fix this by taking advantage of Unity’s built-in Layers and Tags. If you open up the Knight prefab, there are two drop-down menus below the name field....

Subscribe To My Mailing List

Want to learn how to make a game? Not sure where to start? Even if you are a seasoned game maker there is still a lot you can learn from my mailing list. I'll be covering tips and tricks for how to build, release and market games each month.

Simply sign up for my mailing list and also get access to a 50% off discount code for my eBooks and other content. I promise to not spam your inbox!

Join Now