Download

3rd Person Camera - Follow player

Hey all,

So I was wondering if anyone on here could explain how to adjust the C++ 3rd person camera to always follow behind the player? Currently when you walk left or right, the whole character model rotates and the camera remains facing forward. I want the camera to rotate and the character to remain facing forward. In fact, Ideally, I’d love the player to be able to rotate around the character model as well while holding down the ALT key or something but I’d settle for just the camera following.
I’m sure there would be a lot of other people who would love to see a tutorial on how to do this.

Thanks!

To have the camera following the character, you need to make sure the character is using the controller rotation; this is set by doing the follow (inside the character constructor in the .cpp file):



bUseControllerRotationPitch = false; // 'true' will set the character's Pitch to rotate based on input; 'false' will turn off character Pitch rotation via input.
bUseControllerRotationYaw = true;    // 'true' will set the character's Yaw to rotate based on input; 'false' will turn off character Yaw rotation via input.
bUseControllerRotationRoll = false;  // 'true' will set the character's Roll to rotate based on input; 'false' will turn off character Roll rotation via input.


You will want to use the Controller’s Yaw for the character rotating left/right. You will likely want to have Pitch set to ‘false’ as it would lean your character forward/backward via input if set to ‘true’; Roll will lean the character left/right.

NOTE: For a fresh Third Person Template project, the above should set the camera to be following the character’s movement. If it does not for your project, make sure to do everything below as well.

You need to make sure the Camera Boom’s rotation inheritance is set to true for each axis you wish to have following the parent component. You can do this inside the character constructor in the .cpp file (make sure it is AFTER the CameraBoom has been created and attached):



CameraBoom->bInheritPitch = true; // This inherits the pitch rotation from the parent of the camera boom.
CameraBoom->bInheritYaw   = true; // This inherits the yaw rotation from the parent of the camera boom.
CameraBoom->bInheritRoll  = true; // This inherits the roll rotation from the parent of the camera boom.


Also, make sure the Camera Boom is using the Controller View Rotation -


CameraBoom->bUseControllerViewRotation = true; // 'true' locks the Camera Boom rotation to follow controller input.

and that absolute rotation is not turned on -


CameraBoom->bAbsoluteRotation = false; // 'true' will set the Camera Boom to rotate independently from its parent (this also means inheritance will not be applied, even if set to 'true').

There is a bit more to change in terms of creating a free-look camera (quite a bit if you do it properly) while moving around but if you are still interested in how to do that, I can write a guide on it.

So if I change all of these values to true, it will only follow the player but not become free look? Its weird, I tried changing these values and it didn’t seem to do anything. It seems like when I run Visual Studio in debug mode, if I make any changes to the code, it doesn’t update in the editor. I thought UE4 was supposed to support code re-compiling without exiting the engine?
FYI this is quite literally the first thing I’ve ever coded in my life. Ever. So I’m a total noob.

Sorry, I had answered another question on inheritance and wasn’t thinking; while that is still valid, the main thing you need to do is set the character to use the controller rotation (specifically the Yaw), I have updated my first reply to include this (in case anyone else sees this thread and only reads my first reply).

EDIT:

You need to make sure you save edited files in Visual Studio as it will not be able to see what you have edited until it has been saved. It will only re-compile in the Editor if you click the ‘Compile’ button and wait for it to compile your changes; then you can click ‘Play’ and it should be updated with your changes.
Although this should work fine, I (and a few people I work with) have found compiling small changes via the Editor to cause unusual problems for no reason which resolve themselves if I close out of the Editor and re-run it. For this reason, I avoid using the Editor’s compile feature as I do not want to waste time looking for bugs that do not exist.

That’s alright, setting up the camera to follow should be simple enough for you.
I take it you are planning to learn how to code with UE4? If not, I would suggest using Blueprints as it will be a lot easier/quicker for you to prototype and create things; but, if you are:

  • Learn the fundamentals of C++ first so that you understand what you are reading (‘If’/‘Else’ statements, ‘For’ loops, creating classes with member functions and variables, calling functions, function and variable scope, etc).
  • Pointers are also a fundamental aspect of C++ and are very important to understand but learn the other fundamentals before trying to understand Pointers; people usually struggle to understand Pointers and they should be easier for you to comprehend if you already understand how the basics work.
  • Try to break tasks up into small, bearable chunks. For example, if you want to create a gun that you can shoot and have it damage other characters, you would split that up into the following sub-tasks:

- shooting from the barrel's position and down the forward vector
- checking if the bullet collided with a character
- applying damage to the character the bullet collided with
Create one sub-task at a time and test that it works correctly, with no bugs or issues, before moving to the next part (instead of trying to do all three at once).  This means that if something goes wrong, rather than trying to find out which part of the task failed and why, you will only have to check the sub-task you are working on.

Thanks for all the help man. Yes, I am planning to learn how to code for UE4. Blueprints is pretty much the same as UEscript just visual based and I want to learn C++. I do mostly understand the basics (If/Else, For Loops etc.) as I’ve been watching some good video tutorials but its still all very new. I’m an artist traditionally. I’ve got an entire DVD on programming for games from 3DBuzz but sadly it uses visual studio 2003 which I’ve noticed uses different syntax than 2013 so I’ll have to load up the older version in order to follow along. Still it should teach me all of the basic terminology and concepts so that I can learn everything current more easily.