Download the Starter Project
Layout uses one Master Starter Project. If you’ve downloaded it recently, chances are you don’t need to re-download it.
This tutorial requires that you’ve downloaded the starter project as of 4/27/16:
- Click here to download the Layout Master starter project.
- Extract the zip folder.
- Open up the Layout_ToyBoxx.uproject and begin your adventure!
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. BP_Remote and BP_BlastingBox allow the developer to add actors to an Interact With list from the details panel. When the player overlaps, the remote/box will attempt to call an interface function on each actor in the list.
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 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 PhysicsHandle component.
The actors we pick up will be attahed relative to PhysicsHandle. This means that we need to update the location of PhysicsHandle everyframe.
Challenge:
On Tick, update the PhysicsHandle 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 PhysicsHandle.
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 UE4 has to offer here. Just remember that these functions only effect objects that simulate physics.
In this assignment, 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 UE4.
Consider extending the gravity gun with additional behavior. Also consider using a player perspective other than first person.