In this video, Jesse Freeman teaches you how to create an obstacle for your game. You'll learn the basics of creating a new GameObject from a sprite and how to turn it into a prefab in Unity. Once you have a prefab it will be reusable. Learning how work with GameObjects in Unity 5 is an important skills for building a 2D game.
- [Voiceover] 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 out 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 it's 0,0 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 0,0 position will be here at the bottom. Now when we put our spawner here in the middle of the floor. The chair's 0,0 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 these obstacles. Because this is going to represent a few different obstacles in the game. Next, we're going to 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 zero. But it also makes 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 going to 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 z, position is zero. If we look at the foreground, we see that it's also set to zero. Let's change the foregrounds z value to one.
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 obstacles 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.
Start watching to learn how to convert artwork into sprites, create repeating background textures with Unity's TexturePacker, and build reusable game objects such as players, obstacles, and enemies. Jesse also covers the basics of object pooling, working with custom UI components, and multiplatform publishing.
- Importing artwork
- Editing sprites
- Creating repeating background textures
- Building reusable obstacles
- Recycling game objects
- Building an object pool
- Creating the player
- Starting and ending the game
- Displaying and saving the score
- Adding menus and text
- Publishing a Unity game in different formats