How to carry an object in a 2D game?

Hey all!

I’m currently making a mostly 2D game in Unreal 5.2, which has a 2D pixel art style for the character and everything he can interact with, but a 3D environment (just a bit of context incase this helps!^^)

I’m trying to implement a feature which allows the player to pick up and carry objects, but I keep getting stuck because the character is 2D.

Every tutorial I find always attaches the object to a bone, and my 2D character doesn’t have any bones. This causes my character to not be able to pick anything up.

Any pointers in the right direction are much appreciated! I’m quite a beginner with unreal, so explaining things in simple terms/ a little more detailed is much appreciated! ^^

Here are the two tutorials that are the kind of thing I’m going for:

How To Pick Up And Drop Items | Hold Items In Hand - Unreal Engine Tutorial - YouTube - Matt Aspland Tutorial

How to Carry Physical Objects in Unreal Engine 5 - YouTube - Gorka Games Tutorial

Thanks for reading! ^^

Hey @MeadowD!

So what I usually do is substitute that bone socket with a “scene node” placed above the character’s head. Just add that in the component window, move it where you want it to be relative to the character (it’ll be invisible in game), drag it into the event graph as “get” and use that as your attachment point. Other than that the functionality - meaning the “attach” et cetera - is pretty much the exact same.

If you’re wanting more specific ideas to your project, you’ll need to provide some screenshots or video! :slight_smile:

Let us know either way!

Thanks @Mind-Brain !

The Level Node worked perfectly as a substitute for the bones, thank you so much!! ^^

I have one more question though!

( Also, small note incase this is useful, but I decided to follow the Matt Aspland tutorial, as the Gorka one allowed the player to pick up anything with physics, and that wasn’t what I wanted for my game! )

When my character drops the item, it either goes in front of the (as desired) or behind them, depending on which way they’re facing. This is because I set the X location to 150, but I am not sure how to have it just drop whichever way he is facing (either 150 or -150)

(Pick Up Code)

(Drop Code)

The following leads to this outcome with the player dropping the cube, but in the same position (regardless of player facing direction)

Drop Item Example 1

However, following the tutorial exactly leads to this outcome, where the cube is always dropped underneath the player and even elevates them slightly:

Drop Item Example 2

Here is a piece of code I tried, however it didn’t work, but I’ll include it in case it’s helpful in any way!

(Attempted Fix - Didn’t work though and not currently active)

In both examples, the cube also merges into the floor, and I’m not sure how to fix this either. When I tick ‘simulate physics’, the cube will disappear when touched (not sure why), but there might be some other way to fix this issue?

Thanks so much for all your help, and I hope these screenshots are useful!

If you need any more screenshots, don’t hesitate to let me know! ^^

Hey again @MeadowD!

So as far as your object merging with the ground:
Here is where you’re getting the location of where the block will be set. The thing is, when you set the block to that location, the center of the block will go there… But that’s the floor surface, so it will be set down into the ground by exactly half of the block’s size (no matter how big the block is).
One way to get around this is to make the block’s “Center” be at the bottom of the sprite/object, you would just move the static mesh upwards in the Z axis to do this, within the block actor.

Have you tried not simulating physics, but making it affected by gravity?

Also the block static mesh has to have collision. If you have not given it collision and there are physics or gravity, it will fall through the floor. Give the box actor a Box Collider component and size it to the size of the block and that will stop :slight_smile:

@Mind-Brain Thanks so much for all your help!

Adjusting the centre point of the sprite to ‘centre bottom’ rather than ‘centre centre’, like for the player character, worked perfectly for the clipping into the floor issue!

I have two more, smaller questions though if that’s okay! ^^

Firstly, what would you recommend replacing this section of code with to get my desired outcome? Any pointers in the right direction are much appreciated!

I’m trying to get the player to drop the item in front of themself, but am not sure how to determine the way they are facing and how that would translate into code!

I also have a slight visual issue:

Current Carry and Drop

When the player is facing to the left side of the screen, the cube is behind their hands, but when they are facing to the right, it is in front. I have a feeling that this solution is going to be similar to the previous issue, but I cant seem to crack it!

Also, a side note about the cube itself, ‘simulate physics’ is still turned off, but ‘effected by gravity’ has been turned on this whole time, but doesn’t seem to change anything?

These are all pretty much cosmetic issues though, thanks for all your help with the development and implementation!! ^^ :smiling_face:

Hey Meadow!

Could you give a couple screenshots showing that? Also, it may depend on your spritesheet, regarding the “behind the hands/in hands.”

Also… The way you have it set up may only require minor tweaking looking at this snippet here.

You’re on the right track. But what I would do after the Compare float is set a bool “IsLastMoveLeft?” based on your “Compare Float.” You may want to do this as part of the character’s movement code instead of here. This way it stores the value. Because if you stand still, it’s going to give ==.

Then, when this code is run, you would just use a branch node in its place using the “IsLastMoveLeft?” boolean :smiley:

@MeadowD and @Mind-Brain :

Just some additional info about attaching stuff in 2D. Since you are using Sprites and Flipbooks, you can attach sockets to your Sprites, then you can place and rotate them freely on your sprites, whereever you want (even in front or behind your character sprite), and if you make a flipbook, those sockets get carried over and still exist in the flipbook and are accessible.

So you can attach whatever you want to carry to those sprite sockets.

Those two arrow animations wwer done via socket position and rotation changes on each sprite of their flipbook. Sprites for walking right got their sockets moved up and down, left got some rotation.

I admit, the left arrow animation is a little bit… unlucky ^.^


Sorry for the radio-silence! I’ve been super busy, but I finally have the time to reply! ^^

I made a video to show the current state of the mechanic, as it’s probably easier to see it in-action rather than through descriptions or images!

Link to the video!

I was able to make the player drop the item each side using this code (in case anyone with the same issue finds this thread!)

I currently have two dilemmas though:

I really want to simulate physics on the object, but when I tick simulate physics, I get this error message:

I also have an issue with the facing direction causing the object to appear both in-front and behind the player (This is in the video, as it is a bit difficult to explain! ^^) I attempted to fix it with this code:

However, it didn’t work as I realise you would have to also detach and reattach the object each time the player turns, which I’m not sure would work.

Thanks for everything, and I’m sorry again for the silence on my side!! ^^

(Also, sorry for the extra ping! I was using my twin’s laptop and accidentally sent the question through their account, and then deleted it!)

I just had a pretty good idea for a solution for the animation problem, but might need a little direction for the implementation!

I’m thinking about having it so when the player picks up the object, it technically turns invisible, and instead plays an animation of the player already holding it? This way, it would look a lot smoother, and the object would be able to move as the player breathes, rather than remaining completely static.

Then , when the player drops the object, the animations would revert to normal, and the object would turn visible, and fall to the floor?

I think this would solve a lot of my issues I’m having, but I’m not 100% sure how it would implement into code!^^

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.