In this lesson, we’ll experiment with physically enabled Actors in our physics playground. Cannons, rockets, conveyors, balloons, dominoes - they’re all made possible with physics simulation.
Simulation offers incredible out of the box functionality. We can easily control the behavior of physically enabled Actors and components through their public variables. Enabling physics is easy. Getting physics to “feel right” is an art, and effective use requires some care.
Later, we’ll build the Gravity Gun, a tool used to interact with physics objects. The gun is probably the most sophisticated scripting tutorial of the entire series. There are great opportunities to adapt and extend the gun to whatever end best fits your experiences.
The Physics level includes many physically enabled Actors that you can interact with as BP_PhysicsPlayer. Some Actors are directly interactable by collision with the player or other Actors.
Other Actors like BP_Cannon are interactable via the BPI_Physics_Interact interface. By walking over the multiple BP_TriggerPlatforms in the level, the platform will attempt to call the PhysicsInteract function on its corresponding Actor. You can see how this was implemented in the Level Blueprint.
Take a minute to press Play and explore the level.
In order to activate physics and use physics nodes, we have to tell an Actor and/or component to simulate physics.
Unless otherwise specified, a force is applied to Actors according to their mass. By default, mass is calculated as a function of mesh volume.
Let’s give the player a way to explode Actors in the level.
We can start with a Blueprint that fires a radial force impulse and destroys itself. We’ll spawn this Actor from the player Blueprint.
Our Radial Force component is rigged to explode. Now we need to fire the impulse and destroy the Actor.
Let’s create a new input action mapping for spawning BP_RadialImpulse.
Now we can use the new InputAction PrimaryAction event in our player Blueprint.
Challenge:
Wire the InputAction PrimaryAction event to a line trace from the camera and spawn BP_RadialImpulse at the location of the trace hit.
Head over to the anvil pit and try your new ability. Can you feel the power?!
Our explosions would look much cooler in slow motion. Let’s create a new action mapping for a slo-mo super power.
Enabling slow motion is actually quite easy. We just need to use a node called Set Global Time Dilation.
Test it out to feel THE POWER!
Let’s take a quick diversion from player super powers.
You might have noticed that our conveyors don’t actually move objects. We can write a new Blueprint script inside of BP_TutorialConveyor to get physically enabled Actors moving.
We need some way to determine which objects should be moved by BP_TutorialConveyor. A collision box will tell us when objects are within a reasonable distance of the conveyor belt.
Let’s add OnComponentBeginOverlap and OnComponentEndOverlap events for our collision box.
We need to store all components that overlap our box in a new array variable.
You may be wondering why we’re storing components instead of Actors. This is because we apply forces to components and not Actors.
OnComponentBeginOverlap > Other CompAdd
Great! This is everything we need to add and remove components from the array.
Now we need to add a force to each component on Event Tick.
We need to cast Array Element to a static mech component.
When the cast is successful, we should add a force to the component in the direction of conveyor belt movement. We can specify this direction with an Arrow component.
The Arrow component is a useful tool for development. It will not show up in game unless otherwise specified.
We’re nearly done! Let’s add our last function now.
Targeted components respond according to their mass when Accel Change is not set to true. Experiment with this variable later to see what effct this has on different components.
Challenge:
Set Force by multiplying Arrow’s forward vector with a scalar power value.
When you play the level, physically enabled objects should respond to BP_TutorialConveyor. Choo-choo! All aboard the physics train!
Let’s pick up and launch physically enabled Actors with the Gravity Gun. When complete, the gun will pick up Actors when selected and fire them away from the player.
First create a new action mapping. This method will handle picking up and launching Actors.
When we select an Actor, we should snap it in front of the player.
SnapPoint is now attached relative to the position of the player’s camera.
We can pick up physics Actors with a Physics Handle component.
The Actors we pick up will be attahed relative to Physics Handle. This means that we need to update the location of Physics Handle everyframe.
Challenge:
On Event Tick, update the Physics Handle component’s location using Set Target Location. Set the location to SnapPoint’s world location.
With that done, we’re ready to attach selected physics Actors to Physics Handle.
Like we’ve done so many times before, we need to trace from the player. In this case we only want to trace for physically enabled Actors. We can do this with the Line Trace For Objects node.
We need to store the hit component in a new variable that we can reference later.
Fantastic! Your gun can now pick up targeted Actors.
Let’s launch picked up physics Actors. We can determine whether or not an Actor is currently picked up with a boolean variable.
When we fire SecondaryAction, IsGunEngaged will tell us if we should Pick Up Object or launch an existing Actor.
To launch an object, we must first release it, then add a physics impulse.
Challenge:
Our impulse should be in the direction of the camera’s forward vector and have a sufficiently large magnitude.
We need to reset our variables after releasing the held Actor.
Whew - that was a lot of scripting! You now have a fully functional gravity gun that picks up and launches physics objects. You also have a generalizable workflow for selecting and manipulating Actors in a scene.
There are many other useful physics nodes we didn’t cover in this tutorial. Explore all that UE5 has to offer here. Just remember that these functions only affect objects that simulate physics.
In this miniProject, you’ll create a new experience that incorporates physics. Your experience should be developed in response to one of the following themes:
Your submission must meaningfully include physically enabled Actors and/or components. The submission should demonstrate your understanding of physics in UE5.
Consider extending the gravity gun with additional behavior. Also consider using a player perspective other than first person.
Not sure what the any of the themes mean? Do a quick Google search to find out!
This miniProject is worth 10% of your Assignment Component. For HW_Physics, the breakdown of the marks is as such:
Breakdown / Grading | Great | Okay | Bad |
---|---|---|---|
Requirements (40%) |
Implemented physics meaningfully. | Implemented physics. | Did not implement physics. |
Performance (10%) |
The experience runs at a consistent 75 fps. | The experience has occasional judders. | The experience judders frequently. |
Meshes/Materials (10%) |
All meshes have appropriate materials and are placed with purpose. | Most meshes have appropriate materials and are placed with purpose. | The project is riddled with out of place materials/meshes. |
Audio (10%) |
Audio enhances the experience meaningfully. | Audio is secondary to the rest of the experience. | Audio is hardly (or not) present. |
Experiments (10%) |
The student has experimented to create a unique experience that includes a novel or intriguing perspective and locomotion technique. | The student has created an interesting experience but does not seem to have considered creating something unique. | The perspective and locomotion techniques are unchanged and the experience is bland. |
Creativity (20%) |
Fits one of the themes creatively. | Fits one of the themes. | Does not fit any of the themes. |
Should you need any further clarification, feel free to contact the TA marking this miniProject.
If you do not have a VR headset, that is fine. Just design the level as per normal and "imagine" what it would look like with the headset. Your TAs will not penalise you on this.
For this miniProject, you will need to set the Game Default Map to HW_Physics by clicking on Edit > Project Settings > Maps & Modesand build the project for Windows by clicking on Platforms > Windows > Package Project in the Toolbar.
You must be running Unreal Engine 5 on Windows (and not on any other operating system) in order to build the project for Windows. If you do not have a computer running Windows, save a copy of your project online or on an external drive and use one of the computers found in Media Teaching Lab 2A (AS6 04-26) to build the project.
After building the project, compress its contents as a .zip file titled as [YourMatricNumber]_[YourFullName] (e.g. A0123456Z_JohnSmith.zip).
If you do not follow the submission guidelines exactly, your marks may get deducted!