In this Unreal Engine 4 tutorial I will talk about how to change the “default camera”.
For one of my projects (A 2D side scroller) I wanted to have a camera actor as opposed to a camera component attached to a player character, like in some of the engine sample projects.
I also wanted to have full control of the behavior of the camera and I did not want to worry about the camera after the character was destroyed. If you think about it, if the camera is attached to the character as a component, once the character is destroyed so is the camera.
Here is how I went about getting it done. Keep in mind that this is just one way to go about it.
The GameMode Blueprint
GameMode blueprint event
BeginPlay we loop through all the actors or type
DefaultCamera and look for the one also tagged
DefaultCamera and store a reference to it. We then call
Set View Target with Blend to “aim” the camera at the player. Looping through all the actors of type
DefaultCamera may seem overkill but it allows me to re-use the class but have only one be the main or default one.
My apologies for picking a tag name that is also the name of the blueprint.
MainCamerawould have been a better choice.
DefaultCamera is a simple blueprint I created to program the logic of the camera movement.
The Camera Blueprint
This is a simple camera that fallows the character around ignoring the Z axis (Up) changes of the character position so if the character jumps the camera does not. Your camera can do whatever you want.
While I chose to adjust the camera under the
GameMode, you can also do it in the
BeginPlay event of the actor containing the camera component.
For this game, after the character dies, the camera stops moving (since there is no character to follow) and a retry UI is presented. If the player desires to play again then we reload the level.