Create a Total War RTS Camera in Unreal Engine

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone today we are building an RTS  camera in unre engine using C++ and enhanced   input actions similar to the classic camera  in the total war Series this camera enables   movement in all directions using the W ASD keys  and rotation by holding the middle Mouse button   we will start with the basic setup using the top-  down starter template in unre engine remember to   enable C++ in your project our build requires  two classes a player pawn and a game mode class   typically the game mode class is included  in the starter templates but creating your   own is straightforward if needed first let's  develop our own camera Pawn name it RTS camera Pawn setting setting up the game mode is simple   its primary function is to designate the  camera pawn as the default player pawn [Music] class as you can see you can utilize  the F class finder utility to search for a   dedicated blueprint implementing your class this  is actually a good option to tweak and customize   your camera later on please keep in mind to name  your blueprint exactly as specified in the game mode after compiling the project  our custom C++ class replaces the   template Pawn when you start the game your  camera is centered in the origin in the world now let's go one step further and create a   blueprint class search for your  created class to inherit from it unfortunately as the blueprint didn't exist  before it will not be used by the current game   instance until we either restart or recompile  the game you can compile the project again   through the live coding feature or just  close unreal and recompile it through the editor the game should then use  the blueprint class as our default pawn now let's actually dive into implementing  our own camera we will start by moving the tick   and setup input functions to the protected  section of the class both of these functions   are defined as protected in the parent  class so they shouldn't be public in our   class either let's begin by implementing the  eases function the begin play any real time   strategy game needs to show the mouse cursor the  begin play is the perfect location to enable the   mouse here we can find the control of our  class and cast it to the player controller   the player controller contains a function to  show or hide the mouse cursor next up we can   start implementing The Tick function The Tick  function will handle camera movement based on   user input for this we need a variable to set  the current keyboard based movement speed we   can also create a variable for the player  controller as we already initialized the   player controller in the begin play it's more  perform if we don't do it again in the tick function as you can see you can also declare  variables inside condition in C++ this is   really useful if the variable is only used  inside the condition and it acts like a guard we will prioritize keyboard inputs  like the W ASD Keys over the mouse based   movement only if the keyboard-based movement  speed is zero we will get the mouse position   and calculate if there needs to be any movement  so Mouse movement is determined by the cursor's   proximity to the viewports edges with a  customizable padding you can calculate   it by getting the size of the viewport and  defining a padding where the mouse is near the edge let's say if the mouse is on the left side  of the screen and it is between 0o and 50 pixels   then we need to move to the left if the mouse  is on the right side and it is greater than   the viewport with minus some padding then  move move the camera to the right the same   applies to moving forward by checking the mouse  position y AIS and the viewport height for the   padding we should create a variable so you  can easily customize the screen edges later on for movement calculations we should exclude  the z-axis as our camera operates on a 2d   plane and we will reserve the  zaxis movement for the mouse wheel the new forward location is then  calculated by using the forward vector   and the input movement speed multiplied by  a custom defined movement speed and Delta time likewise the same applies again for moving sideways calculate the new location by  adding both the forward and the sideways locations and finally let's also create a  new variable for the movement speed of the camera as a default movement speed I use 500  on both X but we will probably need to modify   these values later on when we get a feel  of how fast or slow 500 even is let's also   set a default screen Edge padding of 50 pixels  per site now your project should allow camera   movement via moving the mouse to the edges  of the screen uh you can always adjust the   padding for better responsiveness you can  probably also use a padding of maybe 100   pixels or 200 if 50 feels too narrow okay in  order to easily switch between the header and   the implementation file let's also move the  header to the right side of the screen next   up comes the fun part the enhanced input actions  we will add the mapping context requiring a cast   from the local players input subsystem to the  enhanced local player subsystem and prepare for   event handling from input actions let's also add  a variable for the mapping context so we can add   the mapping context through the blueprint  and customize our keys through the unreal editor okay nice now the enhanced input  system is actually initialized but nothing   would happen we need to react to  the events provided by the input actions therefore let's cast and get a reference  to the enhanced input component of our player   also please make sure that you include both of  the enhanced input component and a subsystem header the W ASD keys will be our move action  let's react to them being pressed by adding event   bindings for the triggered and completed events  the triggered function will be called when any   of the W ASD keys are pressed and the completed  will be caught when they are all released let's   also create a binding for zooming we will use the  mouse wheel to change the height of the camera I   will call it camera height action in this case  because I think Zoom is not the correct word for   this but you can also call it Zoom or any other  name we will only need to triggered event for this okay let's also add a  functionality to rotate the camera let's use the started cancer and completed  events for this as quickly pressing the middle   Mouse button would also result in a canceled event  and we don't want to be deadlocked in a rotating State The Last Action that is needed is  a look action this is actually the place   where we rotate our camera if the middle Mouse  button was pressed at all okay we have bound   our events but didn't actually create any event  function let's do it it now the triggered events   provide an input action value as a parameter  the cancer and completed events are without any [Music] parameter after creating the event  functions we also need to create the Action   Properties these will be then configured  through the unre editor and assigned to   our blueprint all of these actions are are  just basic object pointers with a U property specifier awesome last but not least  let's also create our event functions   on the CPP site when using Ryder you can  easily generate the function definitions   when using visual studio code you need to  unfortunately create them manually before   we get to the final implementation let's set  up our enhanced input actions through the editor first up create an input mapping context also create a new folder for our RTS input actions then create four actions for the  move Look rotate and Camera height action for both of the move and look  action change the value type to xess 2D and for the camera High change it to exess 1D   as the mouse we is just the  float between minus one and one okay next up let's create the  mapping for these actions in the   mapping context unfortunately the names  for my input actions overlap with the   starter template so I decided to rename  my input actions to also include the name RTS okay great for the move action I  assigned the W ASD Keys take care   when assigning the correct modifiers to  these Keys there's a great guide on the   unre engine documentation I will link in the  description below which goes through this exact configuration awesome the camera height is easy to  set up as it's just using a mouse wheel access I use the negate modifier here as  it feels weird if scrolling down doesn't   move the camera upwards but you can also  check if the negate modifier is actually   needed in your case for enabling  the rotation itself we just use the   middle Mouse button and for the mouse  look we only need to use the 2D Mouse AIS okay the mapping context is now configured but  we still need to assign both of the context   and the input actions to our blueprint  so let's just assign it quickly one by one now we can finally come to the last  step of this tutorial which is the   actually implementation of the input event  functions and these are surprisingly easy   the move action triggered event will  just set the current input movement   speed to the provided parameter value  and the completed one will reset it to zero for the camera height action we  just add the input value to the z-axis   of our Pawn's location and for this we need  another variable for the ACT Zoom speed also   we don't have access to the tick Delta time  so we would need to use the apps Dela time here let's create the camera height Zoom speed  variable and set a default value in the con structor for the rotation we create yet  another variable which we call should   rotate this will be a Boolean and we can  use this in the look triggered function   as a guard to enable the rotation if  the middle Mouse button was pressed T so for the last part the actual look action  using the middle Mouse button we can get the   look Vector from the input parameter if the middle  Mouse button is pressed and therefore they should   rotate as two we can rotate the camera by  adding the look Vector to the pitch and jaw for this we need the last variable a rotation  speed which I just set to 50° in this [Music] case and last but not least we should take  care of the Constructor because some essential   parts are yet missing let's initialize  our camera to have a tilted rotation of 30° let's also create a default scene route  for our pawn and attach a camera component to it don't forget to create a U  property variable for the camera component now with this done we  can finally test our RTS camera camera unfortunately there's a bug in our code and   this is due to the life cycle of our  class and when certain functions are called unfortunately the begin play  function is called after the setup of   the player input component so accessing the  player controller results in a null pointer   exception let's fix this by getting the player  controller in the setup player input component finally our camera works like the Legacy camera of  haunted Total War games for the last touches you   can adjust the blueprint values and the camera  height to improve the feeling of the [Music] camera I hope this video was useful  and if you have any questions   please don't hesitate to ask them through the comments thank you so much for  watching please don't forget to   subscribe and like the video  and see you in the next one
Info
Channel: Scripted Adventure
Views: 3,708
Rating: undefined out of 5
Keywords: unreal engine, ue5, gamedev, game dev, c++, enhanced input actions, rts, real time strategy, gaming, tutorial
Id: jHj89zNZbyg
Channel Id: undefined
Length: 18min 29sec (1109 seconds)
Published: Tue Feb 13 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.