Pawn Camera Rotation

A free video tutorial from Stephen Ulibarri
Engineer, Programmer, Game Developer, Author
Rating: 4.7 out of 5Instructor rating
14 courses
433,311 students
Pawn Camera Rotation

Lecture description

We have programmed movement input, now it's time to program the ability to control the camera.

Learn more from the full course

Unreal Engine 4 C++ The Ultimate Game Developer Course

Learn how to develop, code and package a complete video game in Unreal Engine

34:27:15 of on-demand video • Updated November 2021

How to program in Unreal Engine with C++.
How to create a complete UE4 game from start to finish.
Those with no experience in Unreal Engine will master Unreal.
Combat, adventure game mechanics, platforms, game saving/loading and menu design.
Those already experienced in game development will further expand their skills.
Those who already develop in another engine such as Unity will master Unreal.
English [Auto]
OK, so we've got a collider, BP, which is a pond, and we've got the ability to move it around using our WASC keys and up, right, left and down, and we turned our critter into this sort of static but animated creature that we can move around. So it's quite simple, but we're increasing our functionality little by little. And thanks to our custom movement component that we created for this pond, we can slide along objects and things like that, and we have the capability to move. Now, what we don't have yet is the ability to look around in the world. Our camera view is sort of fixed at this this angle just looking down at the pond. But what we'd like to do is be able to look around and rotate our camera around the pond. So to do that, we're going to add a couple of new input axes and these are going to be for the mouse. So we'll go to edit and project settings and we're going to scroll down to input here and under access mappings. Let's go ahead and close up those two and we'll go ahead and click the plus sign a couple of times. And on the first one, we're going to call this one camera pitch. And the second one we're going to call. Camera, yeah, and pitch and your, ah, rotation about certain axes. So, again, the pitch would be, you know, if you're nodding your head, yes, up and down, you're changing the pitch of your head. And if you're looking, you're about to cross the street and you're looking both ways, that would be rotating your heads. Ya so that's one way to think about it. So camera pitch, we're going to map this to the mouse and we're going to make the since it's pitch going up and down, we're going to map it to the mouse y axis and for the camera we're going to map this one to the mouse X and so that's how you map these to the mouse. So whenever the mouse is moving left and right, we're going to get a value for a camera. And when the mouse is moving up and down, we'll get a value for camera pitch. And of course, moving the mouse all around will change both of these simultaneously. So we've got our inputs. Let's go into Visual Studio and in Collider DOT S.P.. We're going to bind the axes for this and we can go ahead and just sort of copy these and paste them here. And instead of move forward, we're going to say camera pitch. And instead of move, right, we'll say camera. Yeah. Now let's go ahead and create a couple of functions for these and for camera pitch. We're going to create a function called pitch camera. So let's go ahead and just, uh, let's see, just under move forward and move. Right. So we're going to say void pitch camera. And for our our input access, we can call this anything we want. We can call this access value and we'll create another function called your camera. And we'll also use float axis value for the parameter name so we can go ahead and copy these and we're going to define these. So we'll go into Collider at S.P. and we'll scroll down just under. Let's find move forward and move. Right. So just under move. Right. We'll pace these and we'll need to fully qualify their names. So we'll do that and we'll give them bodies. So we'll do that and now we're ready to give them some functionality. Now let's go back up to the BINDE axis under camera pitch and change this function to pitch camera and four camera raw camera will changes to your camera. So now these functions are bound to these axes for the access inputs for those. OK, so let's give these two functions some functionality. What I'd like to do first is create a new variable here. And I'm going to call this. It's it's going to be an F vector to D. And it's going to be called camera input, this is going to be used to store some information about the X and Y axis values. So I'm going to go to Collider at S.P. and go up to the constructor and I'm going to create a initial value for it, camera input equals F vector 2D and I'm going to give it zero that F for both X axis values. So we've got this camera input vector. It's 2D, it's got an X and Y parameter, and I'm going to use pitch, camera and your camera to update those values. So I'm going to say a camera input that y for the pitch is equal to access values. So whatever the mouse returns for this input, it's going to be set for this this variable camera input that y and I'm going to use camera input X to get the access value from the your camera. So basically when the mouse is moving it's X and Y axis values will update camera input, X and Y values respectively. So I guess it would make more sense to put ya up here above pitch just because, you know, X comes before Y typically, at least in my mind. OK, so we've got this camera input X and camera input Y. Now what we can do is update our rotation and the tick function. So I'm going to create an F vector actually in F rotator rather and call this new rotation and I'm going to start it off with get actor rotation. So it's the current rotation, this frame stored in new rotation. And what I'd like to do is update it's yours. So I'm going to say new rotation Diyar plus equals camera input dot x. So the camera inputs X value updated by the mouse's x axis value is going to be added to camera rotation or rather new rotation. Diyar. OK, so what that's going to do is allow us to change the if we if we call said actor rotation, we can set active rotation to new rotation and what that's going to do is rotate the actual actor itself. So let's go ahead and build this with control shift B and go ahead and let this compile and what it's going to do. Let's go back into Unreligious. And what it's going to do is it should allow us to move the mouse left and right and actually change the use of our actor. OK, there's the reload. Let's go ahead and hit play and move our mouse left and right, and you'll see that we can actually rotate around. Now, what about the pitch? Well, the pitch, we're going to change as well. And what we want to change for the pitch is not the actor itself, but the camera spring arm. The camera spring arm can be rotated as well if we if we get our camera spring arm component. So I'm going to make those a little more compact and I'm going to make another effort rotator and call this spring arm rotation. Let's call it new spring arm rotation. And I'm going to set this equal to spring arm. Arm get components rotation. There it is. So this is now set to this frame, the spring arms current rotation. And so what I'm going to do is take new spring arm. Rotation and I'm going to take it's pitch. And plus, equal camera input. Why? And now I can take new and I can take actually the spring arm itself and set world rotation to New Spring arm. Rotation. OK, so we're doing the same thing only instead of the actor itself, we're moving the spring arms, so let's go ahead and build this, OK? We've built it and we can go back and hit play and now we've got rotation, just as we would expect. Now, I would like to take note of one thing. And if we keep that's if we keep rotating down, the camera will go down and the spring Armel Shorten as the camera collides with the ground. But we can actually rotate all the way down here and look up at the pond from the ground. And that's not exactly something that I want to be able to do, because really, it's it's it's not exactly it's something you would like to restrict in a game, because once you go down and look up and you rotate around and you come back, you're sort of out of whack. So I'm going to escape and I'm going to go back. And rather than setting new spring arm rotation pitch equal to this, what I'd like to do is set it equal to and I'm going to use FMF and clamp. So we're going to clamp and what am I going to clamp? I'm going to take first of all, clamp will return. The first thing is the value that you give it. And this is going to be new road. Let's see, new spring arm rotation that. Pitch. Plus equals camera input. Why? So this will actually add camera input Dawai to the pitch and then return it to the first. Basically the first parameter in the list and then the the next values will be the clamp values. So I'd like to clamp this to negative 80 F and negative 15 dot F, and what this will do is it will ensure that new spring on rotation, that pitch will never get below negative 80 F and it will never get higher than negative 15 F, so its pitch will always be angled down at at the the pawn. But we can never actually move that spring arm outside of this Klamt value. So let's go ahead and build this and let's see just how that feels. So we'll go back to on real well hit play and now you'll see that we can move our mouse up and down but we're clamped. We can't go down below or above a certain value. So now we can move around, we can maneuver our way around this this world, and we can move the mouse to take a look at other things we couldn't see before we couldn't see in this corner before. And, you know, it's it's feeling a little bit more like we have more control. OK, so that is how to create rotational functionality for a pawn. We can rotate the camera. Basically, what the camera springform does is it's attached to the root. And so by rotating the route, which is what gets rotated with set active rotation, the camera spring arm rotates along with it because it's a child. And then rather than rotating the pawn itself for the pitch, we're rotating the spring arm itself. It's that way in the future when we have a character like a humanoid or even a creature, some sort of critter moving the pitch will not actually rotate the character itself in the pitch rotational direction, but it will just rotate the spring arm. So we're making lots of progress and stay tuned and we'll move on to some more advanced stuff.