Check out a free preview of the full Game Development with Unity for Web Developers course
The "Player Input Movement" 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 walks through creating a PlayerMovement script to read direction input from the player in OnMove() and applies it to the RollingMovement component. Composing multiple components instead of having one larger component allows for more complex behaviors and reusable code.
Transcript from the "Player Input Movement" Lesson
[00:00:00]
>> The next script that we're going to write is called player movement. So we wrote this component rolling movement, right? Why are we writing another component called player movement? Why don't we just do this in the rolling movement component? Well, the really cool thing about unity is that we can get additional behavior by composing these components.
[00:00:22]
We can put multiple components together to get more complicated behavior and it allows us to have reusable code. You'll remember that we have an opponent in this game, the opponent also has to move around somehow, right? So our opponent is going to have the same rolling movement script applied to it but we're gonna drive that movement direction vector in a different way with a different script.
[00:00:49]
But for the player, we want to do it with input, we want the player to be in control using the keyboard or gamepad. so what player movement does is it reads directional input from the player in a method called on move. And it applies it to the rolling movement component on the same game object.
[00:01:16]
So we're going to use that movement direction vector from the rolling movement component and put input into that. So let's switch back to Unity. Let's create a new script in the scripts folder, so again, in the project window, scripts folder, I can right click, choose Create, and choose C sharp script, and I'm going to call this Player Movement And hit Enter.
[00:01:56]
And then on the player game object, in the inspector here, I will drag over the player movement, script, and maybe added as a component. Let's do it another way. We come down to Add Component. Maybe it worked for you. It's not working for me for some reason on my Mac here, but I can type in player movement and it will add the script that way.
[00:02:26]
So Let's go open this player movement script by double clicking on it. And again there's some stuff that we need some stuff we don't need we are going to keep the Unity engine namespace but deletes the collections namespaces. There's another name space that we need here though, the input system and that's going to access the input system package that I mentioned is already part of the project.
[00:02:53]
So up at the top after UnityEngine, I'm going to type using Unityengine.input system. So there it is. And right now, you'll see that it's grayed out in my editor. And that's because we're not actually using any methods or properties or anything from that namespace just yet. Now we need a couple of variables.
[00:03:25]
So while actually just one variable, we need to access an instance of the rolling movement class, which is going to be that component that's attached to the same game object. And I'm going to call this variable M_rolling movement. For start, I'm just gonna delete that first comment, but I'm going to type my own inside the start method.
[00:03:55]
And I'm just going to say that we need to gather components and in this case, we just need to gather the one rolling movement components. So M_rolling movements and we're going to use that get component method again. This time with the generic type of rolling movement. And we'll put that in angle brackets with parentheses after the angle blackets and end the line with a semi colon.
[00:04:25]
So we have our rolling movement component now that's on this game object. We don't need an update method, so I'll just delete that. But now we're going to type out a special method that uses what's called a callback context and it's part of the input system. So this is going to be a public method with a return type of void.
[00:04:53]
So it's not going to return anything. And we're gonna call this excuse me on move not on movement, so on move. And then the arguments to this method will be of type input action.callback context and we'll just call that context and then open our curly braces here. So what we wanna do is get the movement vector as input and apply it to the directional vector within rolling movement.
[00:05:30]
So let's type that as a comment so we know what we're doing, get the movement vector and apply it to the rolling movement component. So, when you're using something like a joystick on a gamepad or you're using the arrow keys or WASD on a keyboard, you're only moving in a 2D plane, right?
[00:05:58]
It's not a vector three, it is a vector two. There's an x direction and there's a y direction. But there's not this other dimension there. So we're going to call this vector two movement and we want to assign context.read value with a generic type vector 2. Okay, and this is going to be the callback method that we call when we're hitting arrow keys.
[00:06:39]
So we're gonna read the arrow keys, we gonna come in as a vector 2 and then we're gonna grab that as our own variable that we can then use to assign to rolling movements, movement direction, okay? Now movement direction is in 3D we have a vector 2. Right, the movement of this sphere is in 3D space, but we really only want to move it along a 2D plane along the game board.
[00:07:15]
But it needs a vector three. So we'll assign it a new vector three. So we're going to use the constructor for a vector three and we use the new keyword to indicate that this is a new object. And then in the parentheses, it's very simple. We just need an x, y and z.
[00:07:40]
So we're taking this vector two where should we put the x y and z. Well, we really want it to move along the x,z and plant or sorry the x, z plane because in Unity, y is up. So we don't want to move the sphere up or apply a force in the up direction, which would be pretty difficult anyway because we'd be fighting gravity.
[00:08:03]
We want to move it along the x z plane. So we'll say movement.x, okay because there is an x component to this. We'll say 0 f for y or a float value of 0 and then we'll say this is a little confusing movement.y for the z component because a vector two again.It just as x y but we want to use that second value as our z to move it along the z plane.
[00:08:39]
So that's it for this script we can hit Save. We can switch back to Unity. And at this point you might be thinking we can hit the play button and we'll be able to use input and move the player around. There is one more thing we need to do.
[00:09:01]
So you remember when we added the player input component that came with some unity events. We need to accept the input and invoke that callback method that on move method that we just wrote using the keyboard events. So in order to accept the input we need to invoke the callback methods for the move event on the player input component, okay?
[00:09:29]
So we'll make the rest of these events later on when we do Resetting. But let's actually do that. So I will select the player game object here. And it has the player input component and under Events, I will untwirl this little menu here and then we want the player events.
[00:09:57]
So it's kind of nested here and then we have the callback context for move. If I hit the plus button here, I can then add the callback context from the script we just wrote. So, down here you'll see this little target button. If I click that, I can choose from assets or I can choose from scripts in the scene.
[00:10:25]
I want to apply the script from the player game object. So I'll select that, from the scene. And then there is a function that we wanna execute here, right? So, under player movement, we want the dynamic callback context of on move. So now when we hit the play button if everything went well, we should be able to use the WASD keys to actually drive the direction of the sphere and we're driving this using physics All right.
[00:11:15]
So at this point, you should probably hit Save in Unity. You can do that from File, Save or just hit command S or ctrl S and that will save the scene and save the work that you've been doing so far.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops