Join Jesse Freeman for an in-depth discussion in this video Building a reusable obstacle GameObject, part of Unity 5 2D Essential Training (2015).
- So now we're ready to start adding obstacles to our game. Before we actually do that, we're gonna need to clean up some of our sprites. Let's go into the Sprite folder and take a look at the chair. You'll see that the Pivot Point is set to Center. That means its zero-zero position is going to be in the center of the sprite. Now, when we spawn these, we're gonna put the spawner somewhere in the middle of this carpet, and what we don't want to have to do is recalculate where to actually offset the chair or any of the other obstacles so that it matches inside of the carpet.
One way we can get around this is by selecting all of our sprites and changing the Pivot Point to the Bottom. We'll have to hit Apply to change this, and now if we go back to the chair, the zero-zero position will be here at the bottom. Now when we put our spawner here in the middle of the floor, the chair's zero-zero position will be set to where the spawner is, and the rest of the artwork will flow up from there. Now, let's create our first obstacle. We'll drag our desk over into the scene, and let's double click on the desk in order to zoom in on it.
First, let's rename this "Obstacles" because this is gonna represent a few different obstacles in the game. Next, we're gonna want to add a Rigid body onto this obstacle so that we can actually give it a force and move it along. Let's go to our Component, and we can filter out all the different types of Components available by using the Search window. Here, we'll search for a Rigid body 2D. A Rigid body 2D is what actually applies gravity to our object. Since we don't need our obstacles to have gravity, we're gonna change the Gravity Scale to 0.
Let's also make it a Fixed Angle. This way if anything collides with it, it doesn't try to rotate, and finally, let's check Is Kinematic. By selecting this, we're telling the physics engine that this object is a solid body, so when another collider runs into it, it won't move the obstacle out of the way. The goal being when the player hits an obstacle, the obstacle is gonna push the player off of the screen. Now, before we make this move, let's test out how this looks in our scene.
We'll slide this over by the ground. As you can see, the ground is in front of our object. Let's take a look. Our obstacle's Z position is 0. If we look at the Foreground, we see that it's also set to 0. Let's change the Foreground's Z value to 1 so that the desk, and any other object we put in the game, will sit above this layer. Now we can move this up slightly and put it off to the side again.
One last thing that we want to do is make this into a Prefab. A Prefab is a reusable game object that we can configure either in the scene or in our folder, and then when we build our spawner, we'll use that Prefab to create a new instance of the obstacle. Let's go to the root of our Assets folder and create a new folder called Prefab. And now in order to make this into a Prefab, we simply drag the obstacle's game object into the Prefab folder.
You'll see here we now have our Prefab, and also, Obstacles is now highlighted in blue. If we selected this, we'll have a new menu with Select, Revert, and Apply, which allow us to make changes to the object and apply them back to the Prefab or get the default values from the Prefab itself from any instance in the scene. Up next, we'll look into how to move this obstacle across the screen.
Super Zombie Runner riffs off the popular endless runner genre: a one-button casual game that's not only fun to play but exciting to build. The focus of the work is a reusable code base that you can extend and make your own, while learning the basics of object pooling, supporting multiple screen resolutions, tracking scoring, and multi-platform publishing. Along the way, Jesse covers key Unity features, such as working with sprites, creating animations, and leveraging the UI components.
- Working with sprites and animation
- Creating the player
- Spawning GameObjects
- Adding obstacles and collisions
- Building the game objects and logic
- Managing game state
- Creating pixel-perfect cameras
- Designing for multiple screen resolutions
- Adding scores
- Optimizing performance
- Publishing to desktop, web, and mobile platforms