Adding Physics and Keybinds in Unreal
Working with collisions we set our Ray Cast to print out to the output log what Objects we were colliding with within our world. It was also set that we couldn’t collide or register that it had collided with an object if that object type wasn’t set to PhysicsBody. Now we are going to be adding in a Physics Handle component to our default pawn. We are going to be using a Function Template that looks like this FindComponentByClass<>()
to be able to find the Physics Handle Component within our Default Pawn. To start adding in this new component we first look at our DefaultPawn_BP within the Unreal Editor. Within our details of our DefaultPawn_BP we will hit the “Add Component” button and search for “Physics Handle”. By Unreal’s definition the Physics Handle is a utility object that is used for moving physics objects around. This is exactly what we need to be able to move our Cylinder object in the world that is going to be our “key” out of the room. Once the DefaultPawn_BP has both the “Grabber” component and now the “Physics Handle” component we can take a look at our code to make this component functional. First things first, we have to add an #include
in our header file to work with this new Physics Handle. Within our Grabber.h
file we will add
#include "PhysicsEngine/PhysicsHandleComponent.h"
This include lets us use the UPhysicsHandleComponent
from Unreal. We will add this in our private members of our Grabber.h
file as well like so
UPhysicsHandleComponent* PhysicsHandle = nullptr;
We set the PhysicsHandle
to a nullptr
because this ensures that there will be no issues if there was one set in our component already that wasn’t set to nullptr. Unreal will crash without it because there is no way to know if the Physics Handle will “render” in before the “Grabber” component has. So without knowing we have to add a check at our BeginPlay()
function to check for the “Grabber” component as well as the “Physics Handle”. Within our BeginPlay
we know that the new PhysicsHandle
is a pointer to the UPhysicsHandle Component. If we take a look at our Unreal Editor and see how our components are arranged we see that we are working within our “Grabber” component but we want to access that “PhysicsHandle” component. Looking at these two component, the only relationship these two have with each other is the “common ground” per say of having the same Owner. So to get access to our “PhysicsHandle” component within our BeginPlay()
function inside our “Grabber” component we have to first GetOwner
. This is where we are going to use that Function Template that was mentioned earlier FindComponentByClass<>()
. At this point in our code we have a PhysicsHandle
declared in our private members as a UPhysicsHandleComponent
we just have to actually set it within our “Grabber” component now, our BeginPlay()
function will now look like this :
void UGrabber::BeginPlay()
{
Super::BeginPlay(); PhysicsHandle = GetOwner()-> FindComponentByClass<UPhysicsHandleComponent>();}
This function template will return the first component found, because it is a function template the <>
brackets are used to put the name of the component that we are searching for. Now with that set up lets add in a simple test to BeginPlay()
to make sure that the “PhysicsHandle” is working properly and that there won’t be a crash from Unreal for not having it active when the game is ran. We will add a simple if
statement that really only gives a warning if there is no Physics Handle found.
if (PhysicsHandle)
{
// Physics Handle is found
}
else
{
UE_LOG(LogTemp, Error, TEXT("No Physics Handle component found on %s"), *GetOwner()->GetName());
}
This if statement won’t do anything if the Physics Handle component is found it will only put a warning in the output log if the Physics Handle isn’t found.
Now that we have our Physics Handle attached properly within our DefaultPawn_BP, we need to use this Physics Handle when we go to pick up our Object within our world. Right now the Physics Handle isn’t doing anything yet, we need to add functionality to it and that will be coming up, but we also have to set up our keybinds so that the user as the ability to hit a key and actually grab the object. Our game currently has the basic walk keybinds with W, A, S, D, but we need to add a key that when pressed will activate the actual “grabbing” of the object. So first we need to go into our Unreal Editor under the “Settings” > “Project Settings” > “Engine”, and look for “Input”.

Under the “Bindings” at the top we see two options of “Mappings” that we can add. Action Mappings are used for on / off actions. Like grabbing and releasing something, which is what we need. The Axis Mappings are used for other inputs like analog values, so like the analog sticks on a controller. We will be using the Action Mappings, to start we will hit the “+” after it and it will add a drop with a default name set. Lets change that default name to something that we can remember exactly what it is that it is going to be doing, just to make it easier for us and anybody else that might have to look at our code, we will name “GrabAction” because that is essentially what it is going to do, simply grab the object. The second option that it will give you after the name is essentially the keybind, in our simple game we aren’t going to be using a controller, one of the most common “interact” keybinds for a lot of games is “F” so we will set it to that for our keybind. Pressing the “+” after the name box will add a second keybind option because you have the same action triggered by multiple keys. We need to make sure that whichever keybind we set here within the Unreal Editor will have to be the same ones that we use in our Code Editor.
Within our Code Editor now we have to give functionality to our game whenever the user inputs the needed key for a specific action the our character in game performs the way the user has intended. To start, within our code editor we first have to add a new private member to our Grabber.h
file. Under our private members will add in a UInputComponent
if your project doesn’t have it included somewhere else you will need to add
#include “Components/InputComponent.h"
our UInputComponent
will look like this :
UInputComponent* InputComponent = nullptr;
We set this as well to nullptr
for the same reason we did for the PhysicsHandle
to prevent any crashes from Unreal. Back to our Grabber.cpp
file under our BeginPlay()
function we can add in the same idea we did for the Physics Handle, the only relationship between the “Grabber” component and the “Input” component is the Owner.
InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
We use the same function template of FindComponentByClass
but we change what is inside the <>
brackets to search for the Input Component instead. With this in place now we will set up yet another if
statement just to make sure that it is only working when it needs to be working, in this case that it is only working when the designated key for the “Grab Action” is pressed and when it is released.
if (InputComponent)
{
InputComponent->BindAction("GrabAction", IE_Pressed, this, &UGrabber::GrabFunction); InputComponent->BindAction("GrabAction", IE_Released, this, &UGrabber::ReleaseFunction);}
This is a bit of confusing code here lets take a look at the BindAction
function and what arguments it is asking for. The first argument is a string that has to match the name that was entered when we added the new keybind in the Unreal Editor. In our case we had named “GrabAction”, so even it is also being used for a release action the initial name of the keybind action we set was “GrabAction” . The second argument is asking for an InputEvent or a KeyEvent, this in Unreal is the IE_Pressed
, InputEvent Pressed will look for the key being pressed and then IE_Released
, InputEvent Released will look for when the key has been released. The third argument is the object itself (this), which is “this” object the instance of the class that is executing this part of the code. The last and final argument, although looks confusing, is simply asking for an address to a function that will have the functionality needed for when the key is pressed and one for when it is released. In our case our functions are GrabFunction
so when the key is pressed and held down the GrabFunction
will be called and the functionality within it will run. When the key is released the ReleaseFunction
will be called.
This is all right at the point where the next steps left for us in our grand escape from our building is to finally build the functionality to be able to actually grab the object within our world and place it in our Trigger Volume to open the door and let us out. We added a Physics Handle Component to our DefaultPawn_BP so that it can interact with our Objects in the world that have the Object Type of “PhysicsBody”. We also created keybinds and implemented them within our code to be fully functional from receiving user input and using that input to make function calls.