How to grab an object and move around in Unreal Engine 5?

Share this post:

In the previous article, you’ve learnt how to create a mirror material in Unreal Engine. In this article, our student assistant (SHEN Xiao Yuan) shares how to use blueprints in Unreal Engine to allow a third-person character to grab objects.

UE5 - grab object demo

Introduction

Here’s a step-by-step tutorial on how to grab and move an object in Unreal Engine 5 using Blueprints.

Steps

Step 1: Create a New Blueprint Class

  1. Open Unreal Engine 5 and create a new project or open an existing one. 
  1. In the Content Browser, right-click and select Blueprint Class
  1. Choose Actor as the parent class and name it (e.g., Teabag). 

Step 2: Add a Static Mesh

  1. Open Teabag Blueprint. 
  1. In the Components panel, click on Add Component and select Static Mesh
  1. Choose a mesh (like a teabag) in the Details panel to represent your object. 
  1. Add a Sphere Collision component to serve as the grabbing area and adjust its size to encompass the object. 
  1. Compile and save your changes. 

Step 3: Implement Grab Logic

In the Event Graph of Sphere:

  1. Delete any existing nodes.
  2. Find both On Component Begin Overlap and On Component End Overlap events from the Details panel, click “+” to add them.
  3. Since the movement taker is ThirdPersonCharacter, drag a line from Sphere nodes and search for Cast To BP_ThirdPersonCharacter, then set up the nodes to allow input when there’s an overlap between the Sphere Collision and the character:

Step 4: Create Pickup Event

Open your player character Blueprint (e.g., BP_ThirdPersonCharacter).

  1. In the Components panel, click on Add Component and select Static Mesh, naming it (e.g., Teabag).
  2. In the Event Graph, right-click to create a Custom Event (e.g., Pickup).
  3. Drag a line from the event, search for Set Static Mesh, and connect the New Mesh nodes.
  4. Drag the Static Mesh from the Components panel to the Event Graph, assigning it as the Target of Set Static Mesh.

Step 5: Create Input Actions

Go back to the Teabag Blueprint, under the Event Graph:

  1. Right-click to search for a key event (e.g., F for keyboard input).
  2. Link it to the Cast To BP_ThirdPersonCharacter node, add the Pickup Event, and set up nodes to Destroy Actor and play a sound upon picking up, the nodes are showed below:

Step 6: Test Your Setup

  1. Place your grabbable object (e.g., Teabag) in the level.
  2. Play the game, approach the object, and press the designated key to grab and move it around.

Step 7: Fine-Tuning

  1. Adjust the size of the Sphere Collision for better grabbing.
  2. If you want to attach the object to a specific point (like a hand), open the player character Blueprint (e.g., BP_ThirdPersonCharacter):
    • In the Event Graph, drag from Set Static Mesh, search for Attach Component to Component, and specify the Socket Name (e.g., righthand).
    • Drag both your static mesh and the Mesh into the Event Graph, setting the static mesh as Target and Mesh as Parent.

Step 8: Creating a Socket

  1. Double-click the Skeletal Mesh Asset under the Details panel of the Mesh.
  2. Click the SK_Mannequin button at the top, find your desired attachment point (e.g., hand).
  3. Right-click to Add Socket, naming it the same as the one used in Attach Component to Component (e.g., righthand).
  4. Right-click on the new socket, choose Add Preview Asset, find your grabbable object, and adjust its size and position as needed.
  5. If the socket isn’t visible, click Show All Sockets.

Done!

Conclusion

You now have a basic setup for grabbing and moving objects in Unreal Engine 5 using Blueprints. You can extend this functionality with more features like sound effects, animations, or advanced physics interactions as you become more comfortable with Unreal Engine. Happy developing!

 

– By SHEN Xiao Yuan (student from MPhil in Chemical and Biological Engineering)

October 3, 2024

You may also be interested in…