Try reloading this page, or reviewing your browser settings
In this video segment, learn more about the specifics of camera-aligned player movement, like how to select camera, read player input, and move the character based on these actions.
- Camera-aligned movement
- customized axes system
- control system
About this video
- Alan Thorn
- First online
- 12 January 2019
- Online ISBN
- Copyright information
- © Alan Thorn 2019
[Audio Begins] [0:00:00]
Alan Thorn: In this movie, we’re going to consider a practical example of camera aligned player movement. In particular, we’re going to look at several things. First, we’re going to select an alignment in a position for our camera looking out onto the scene. The second, we’re going to read player input. That is up, down, left, and right movement from key presses on the keyboard. And third, we’re going to move the player character based on one, the input itself, and two, the alignment of the camera. In particular, as we move an object around in the scene, we want to move objects either further from or nearer to the camera. Previously, we saw how to move objects around purely based on keyboard movement, and we followed the forward vector to make objects move around. But this time we want to lock the movement of player objects to the alignment of the camera in the scene. Let’s see how to do that.
To demonstrate this, I’m going to be working from the sample scene that we’ve been working on, this outdoor scene with the car parked on the driveway. Right now, we have an isometric camera set up so that when we look at the level inside the game tab, we see everything from an isometric view. If I press the Play button on the toolbar, and I use the A, W, S, and D keys to move the car around, by pressing the W key the car is moving forwards in the direction that it’s looking. And the A and the D keys will turn the car. And the S key will simply reverse the car. In this instance, that might be what I want, but in some instances it might not be. On the other hand, I might want to realign the camera. For example, I might want to align the camera about, well, maybe about there. Select the camera. And then choose Game Object, Align with View. Now, when I do that, we get a very different view of the scene. And the question is, is how should player input relate to the movement of this character. If I press play on the toolbar now and I press the up key or the W key on the keyboard, although the car moves forwards in the direction that it’s looking, I might in fact want the up key to move the key up along here. And the reason for that might simply be because based on where this camera is currently aligned, that kind of input makes sense.
The way I’m going to achieve this first of all is by creating a completely new and empty object. So I’m going to choose Game Object, Create Empty. And this effectively is going to be my input axis. So I’m going to select this kind of input axis along here. And effectively, what this axis is going to do is it’s going to represent the central areas of alignment for my character. So, for example, if my camera is aligned like this, then effectively, by pressing up on the keyboard, the object is going to move up in this direction here. So in this instance, what I want to do with this object here, this input axis, is I want to change its alignment from being oriented this way so that the forward axis moves it in this direction here. So that by pressing it up, I’m moving it this way. So that’s looking pretty good here. So I’m going to set my input axis to be about there.
I’m then going to go to the scripts folder and choose Create and C# script, and I’m going to call this custom control axis. This time I’m going to select the car root object and deactivate the player controller script because this is using the old control system. Instead, I’m going to choose the custom control axis and assign that to the root object. And open the script file here inside visual studio. Now, having done this, I’m going to set the movement speed for this object to be move speed, and the initial speed of one, that is 1 meter per second. We’re going to do our usual trick here which effectively is going to be we grab the transform component, so I’m going to choose this transform. And in addition, I also want to get the rigid body component, which is this RB for rigid body. In the awake function, we’re going to retrieve references to both of those components. So one is going to be get component here. I’m going to get the transform component. Similar thing here with the rigid body. I’m going to choose get component. And I want to go in and we’re going to get the rigid body component.
Inside the update function this time, I’m going to be also reading input from the player, but I also want to know what my input axis is going to be. So for that I’m going to use a completely separate transform component here. So I’m going to choose transform, and I’m going to say input axis. And it’s going to equal the initial value of null. I’m going to leave that set as null, so that from the inspector, we can specify what the input axis are. So I’m going to wait for those fields to be populated and then drag and drop the transform component for the input axis into that field, so that I’ve got a reference to where we’re going to be moving to relative.
So I’m going to go back to my script file, and inside here I’m going to be reading the input from the keyboard. We’ve seen how to do that before, so that’s input, get axis. And for one of the axis, we’re going to be reading the horizontal, and for the other we’re going to be reading the vertical axis. So this one we’re going to be reading vertical, like so. Now, based on the input here, we’re going to control the direction in which this object is going to move. So effectively, I’m going to be selecting the velocity component here. And based on this, I’m going to be effectively specifying where we’re going to be moving both horizontally and vertically. So in terms of the horizontal movement, we going to be effectively using the input axis dot the right vector. We’re going to multiply that by our movement speed. And then we’re also going to multiply that. In fact, we don’t need to multiply that by Time.deltaTime on account of the fact that since we’re using the rigid body component, that is already encoded into it. One thing I want to be careful of here is we want to use the fixed update function. Now, in addition to this one, we also want to take into account vertical input here. So this time I’m going to choose input dot axis, forward, and then once again by the move speed. And press Command-S on the keyboard here to save that here.
I’m going to go back to Unity here and press Play on the toolbar. And once I do that, I can then use the up, down, left, and right arrows to begin to control the movement for this character here. So in fact, already you can see that if I use the movement keys. In fact, there is a problem here. Let me just take a look at what that is inside my script file here. Ah, the problem is that I also need to multiply it, take care here. I also need to multiply it by my horizontal and my vertical values to ensure that everything is going to go in exactly the direction that I want it to. Let’s go back to Unity here.
So then, I can control the car using my input here. So by moving up, I can move the car up. I can move the car down. I can move the car left. And I can move the car right. And notice this time it is in fact based on not the forward vector of the car, but it’s based on the axis here. So that if I did something like rotate this axis, in fact the movement would begin to look quite different because this axis is oriented completely differently. So that’s how we can go ahead and create a customized axis system for controlling the movement and the direction of which objects move.
In this movie, we saw how to create a custom axis that could be aligned to the camera to create a completely different control system by using the input dot get axis functions.
Now, congratulations on reaching the end of this module practical transformations for creating game play inside Unity. Over this course, we’ve seen a lot of critically important information. All of these critical pieces of information here will help you create better game play, better games. I’ve been Alan Thorn, and this has been movement and rotation fundamentals in Unity. Thank you for watching.
[Audio Ends] [0:09:02]