Join Jesse Freeman for an in-depth discussion in this video Input introduction, part of Advanced Unity 2D: Platformer Player Movement.
- View Offline
- When working with inputting Unity for the first time, it can be a little bit daunting. Let's take a look at how to build a simple object, and make it move across the screen by taking advantage of Unity's built-in InputManager. To get started, let's save our scene out. We're gonna create a new folder, called "Scenes", and in this, we're just gonna call this "MovementStaging". Now, whenever we wanna test out new movement, as an example such as this, we have our own self-contained scene to experiment in.
Next, we're gonna need to create an object in order to move. Let's go to GameObjects, 3D Objects, and Quad. You'll notice a quad is just a simple square. It's very similar to a plane, except it's only made of two triangles. If we were to switch to 3D mode, you'll see it disappears, because it only has a texture on one surface. These are really good for using inside of your 2D games in order to test out objects and build out quick demos, like I'm about to show you, for testing movement.
Some things you'll have to keep in mind though, is that since this comes from the 3D side of Unity, things like the Mesh Collider aren't gonna work with the 2D physics engine. So in this case, we're gonna remove this component. In its place, we're gonna add a RigidBody 2D. In order to quickly filter through all the options inside of Unity, simply use the search window. Here I have RigidBody2D, and I'm gonna add this to my game object. Now, we don't want this to actually have real gravity, so we're gonna set the Gravity Scale to zero.
But without the RigidBody, we wouldn't have a way of moving this object left or right by applying a force to it. Next, in our Assets folder, let's create a Scripts folder. In the Scripts folder, we're gonna create a new C# script and we're gonna call this "SimpleMovement". Now, let's open this up inside of MonoDevelop. We're gonna need two properties in order to get this to work. The first is gonna be speed, and the second will be a reference to "body2d". Let's create a public float, called "speed", and we'll make this value five.
Next, let's create a private property. We're gonna make this a type RigidBody, and we're gonna call this "body2d". Previously, in earlier versions of Unity, you actually had access to the RigidBody2D as a property through MonoBehavior. That's being phased out inside of Unity 5, so in this case, we'll just create our own reference. Here, we'll set the value of body2D by getting the component of the RigidBody2D attached to the game object.
Now, inside of Update, we're gonna add the logic we need in order to make this move. We're gonna get started by creating a variable called "val", and this will store the value from the InputManager's axis. Here, if we do Input.GetAxis, we can supply a name to a specific axis we want to get a value of. In this case, we're gonna go for the horizontal axis to make him move left and right. Before we go further, let's take a quick look at how axises are defined inside of Unity.
If we go into the Edit, Project Settings, Input, you'll see here that we have all the axises that are defined by default inside of Unity. Right now, there are 18. If we select "Horizontal", and we go through the properties, you'll see there's a negative button and a positive button, which represent which button makes it move left and right. This is the left key and this is the right key. Now, the way to think of an axis is a value. It's actually a percentage; zero being the center point, and one being all the way to the right, and negative one being all the way to the left.
You can have a range inside of this, something like 0.5 if you're moving halfway. If you're using a controller that has an analog stick, you can move the analog stick slightly to the right and get a smaller value back. But anything over zero means that there's movement inside of the controller. You can tweak these settings, such as the dead zone, the zone that's calculated, the distance from the center point all the way to the edge, and what Unity should actually listen for, the sensitivity, and then gravity.
Gravity is what allows that value to return to zero, once the input has stopped being accepted. Now, if we go back over into MonoDevelop, let's apply a force to our body. So here, we have our body2d. We're gonna set a new velocity. This will be a Vector2 since we're working inside of the 2d engine. We're gonna multiply our speed by the value that we get from the axis. Remember, if it's zero, it's going to not move at all. If it's over zero, it'll just simply take a fraction of speed.
So in this case, one or negative one will make it move left or right. And the last thing we want to do is get a reference to the body2d's velocity.y value, and we'll just set this as default. Now, we'll save our script, go back into Unity, and drag this SimpleMovement script over to our Quad. We'll select our Quad, make sure that our script is there, and now if we run the game, you'll see that we can use the left and right arrows to move the box.
Now, this represents the basic, simplest way you can get something to move. But it's actually not ideal to scale, especially when it comes to things like adding multiple controllers, multiple players, or even more complex types of input. So we're gonna look into how to build our own InputManager, and how to have these controls be a little bit more abstracted, so that we can accept input from all different types of values, and also how we can scale it across larger games.
- Building an input manager for keyboard and controllers
- Setting up the player
- Building reusable, self contained, and modular behaviors scripts
- Adding behaviors: walking, running, and jumping
- Managing animation states and player collisions
- Adding a ducking animation
- Detecting wall collisions
- Sticking to, sliding down, and jumping off walls
- Picking up items
- Firing projectiles