While our game may be running without any issues in the editor or even in…
Adding Force to an Actor
Sometimes, we need to apply a force to an Actor. The force we apply is like a “thruster”. It’s good for adding a burst over some (non zero) time and it should be called every frame for the duration of the force. In order to apply a force you need a Primitive Component.
In this tutorial however, I won’t apply the force over some time but instead I’m going to apply it once for demonstration purposes. I’m going to choose an Actor from my Viewport and by pressing a button, I will apply a force so I can make it go a little higher.
Preparing the Character Class
In this tutorial, I’m going to use a FirstPerson C++ Template. To avoid creating a keybind in C++ I will create a function that is going to apply the force and call it from Blueprint, just for prototyping purposes. In order to apply a force to an Actor, we need the direction of the force and of course the corresponding Actor. Open up the header file of your C++ Character and add in the following variables and function:
Next, we need to implement the MoveChosenActor function. To do so, switch to your character’s source file and type in the following implementation:
In case you copied the code above, don’t forget to edit the name of the character’s class to match your own project
Choosing the Actor and setting up the necessary Blueprint
If you followed up this tutorial, you should have the map of the template loaded in your Viewport. To choose the Actor you want to apply force to:
- Select your Character from the World outliner
- Edit the following settings however you wish
Make sure you entered a really high force value so you can detect the difference and that the cube you choose has the option Simulate Physics set to true.
In order to make things work, open up the Blueprint for your character and create the following nodes:
And that’s it. When you press play and press the F key you should be able to see your cube “fly”.
This Post Has 0 Comments