Check out a free preview of the full Game Development with Unity for Web Developers course
The "Player Input" Lesson is part of the full, Game Development with Unity for Web Developers course featured in this preview video. Here's what you'd learn in this lesson:
Nick demonstrates adding components to the Player GameObject including Rigidbody and PlayerInput. Brief discussions regarding mass, drag, interpolation, and input actions are also provided in this segment.
Transcript from the "Player Input" Lesson
[00:00:00]
>> So, we hit the play button. Nothing happened. It's because we need to add some additional components to our game objects. So, now we're going to work on adding these components and we're gonna work on player input. So in order to add player input, we need to first add a couple of components to the player game object.
[00:00:26]
And some of them are built into Unity, some of them are gonna write ourselves. So the first one is what's called a rigid body. So our spheres and the cube already had those collider components that you saw that's so that physics, collisions can actually be detected. But they don't actually have a mass to them.
[00:00:49]
They don't actually have anything that's gonna interact with the gravity that is in a Unity scene. So we need to add a Rigidbody component so that Unity knows this is something that actually needs to drop and, roll around and interact. Then we'll add a player input component, which is part of the input system package.
[00:01:14]
And that will allow us to invoke input events. Have callbacks for those and actually interact with some of the scripts that we're going to write. And then we're going to write two custom scripts or two custom components I should say, that will add to the player game object.
[00:01:32]
One is going to be called rolling movement, and the other is going to be called player movement. And we'll see why When we get to those. So let's go back to Unity. And let's just add the built-in components first. So in the hierarchy, I have the player game object selected.
[00:01:56]
That's the sphere that we created that in the game view is off scene, but in the same view, if I hit F Is right here? And down here at the bottom in the inspector with the player game object selected I'm going to choose Add Component. And do we want to add a rigid body so I could dig through the menus here I could go to physics and find rigid body here or you can just type at the top and it will find things much faster.
[00:02:31]
That's usually how I add component because there are lots of built in components in Unity. So lets add a Rigidbody, don't add a Rigidbody 2D, that's for 2D games, this is a 3D game so we want a normal Rigidbody. And then on Rigidbody, we are gonna set a couple of its properties so it has some mass of 1 by default.
[00:02:58]
We actually want a mass of 0.5. And this is something interesting that you can play with and in the game, after we've built everything if you want to see what, different masses to the spheres might be like or what different movement speeds might be like it's a way that you can tune the game to behave differently than it does.
[00:03:21]
The way we're going to build it. Next we're going to change drag to a value of 2. And that does kind of what the name implies. It simulates air resistance. So that's going to kind of allow these spheres to move a little bit more along kind of an S curve, as things speed up and slow down, it'll look better.
[00:03:46]
And then the last thing we're gonna change on this rigid body is the interpolation. So this is a little bit more advanced. It's something we do need to change here we're going to change the value of interpolate from none to interpolate. And that just kind of changes. Basically the way that objects move from one physics tick to the next or one physics update to the next.
[00:04:17]
It changes how things well, yeah, are interpolated from one update to another. So we're just going to change interpolate to interpolate. [LAUGH] And that will kind of smooth things out. It'll make the movement look a lot smoother. So next we need to add another component called player input.
[00:04:39]
So if I go down here and choose add component and type in player input, you should see it's right there. I'll choose that. And we want to do two things here. First, player input needs a set of actions and I've actually already created an actions asset. So that's not something that we're going to do ourselves.
[00:05:07]
Input is a very deep topic, but I have an actions asset and you'll see that there's this field here. There's a little kind of circled target icon next to it. If I click that, it will bring up a selection window and I can choose things that are in the scene.
[00:05:30]
There's no action assets in the scene to choose from. Or [COUGH] you can choose from assets. So you can choose from the actual folder that you see in the project window here, and I want to choose the one called gameplay. It's the only option. If you double click that it will add that actions asset and now if I actually double click on that this isn't something you need to do, but if you're curious you can double click the actions asset and it will show you this window, which is where I've already set up all of the actions for the game.
[00:06:07]
So we have this move action. I've tied that to WASD and that is how we're going to get keyboard events flowing into our callback methods that will actually drive this sphere around. And then I also have a restart button so on the keyboard I've tied that to the space bar.
[00:06:32]
But I've tested this out once, but theoretically, if you plug in a gamepad you can also play with the gamepad. You can use the left stick to move the sphere around and you can use the south button on the gamepad which is like I wanna say like X on Xbox and I think it's also X on PlayStation I'm not sure but it's the button at the bottom.
[00:06:57]
So that's the asset that we're adding to the player input component. I said there were two things we wanted to do here the other thing is we want to change the behavior to invoke Unity events. And that will bring up this menu here. And you saw some of the actions that I had already set up like move and restart and when we're scripting, we will add some callback to these events.
[00:07:37]
So those are the two things we need to do. To player input we need to add that actions asset and we also need to change the behavior to invoke Unity events
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops