Attach and detach an actor to an other

Hello, I’m currently working on a 1v1 multiplayer game. The game is basically a mix between Baldur’s Gate 3 combat and BloodBowl.

I’m struggling to find a way to attach the ball (an Actor blueprint) to the character’s hand (a Character blueprint).
I thought about creating a socket in the character’s hand. By default, there’s already a ball attached to the hand. I created a Child Actor component in the character’s mesh, with the ball’s blueprint set as the class.

I’ve coded a function to throw it, which detaches the ball from the hand and launches it away.

Right now it’s still in a testing phase, so I haven’t set up collisions yet, which is why it goes through the ground.
But when I try to pick up another ball from the ground, I can’t get a proper result. I’ve tried several methods:

· Set Actor Relative Location

· Attach Actor to Actor

· Attach Actor to Component

I think I’m using the nodes incorrectly even though the solution shouldn’t be that complicated. Does anyone have a fix or advice ?

Attach actor to component is what you want, if you’ve not already, you have to get the mesh component from the character, and reference the bone name Wich can be viewed in the skeletal mesh asset. For attach setting I recommend snap to target, leave rotation relative and keep world scale

Thanks for your reply, but it doesn’t work — I had already tried that.

I’m going to try to describe my game in as much detail as possible to figure out what’s going wrong.

Each player controls 5 characters. When you click on one of your team’s characters, that character is stocked in a variable called ‘Selected Character’ inside my custom PlayerController (BP_PlayerControllerMultiplayer).
When you select a character, a menu appears with different buttons and actions (move, throw, pick up, attack, etc.).

My character has a ChildActor component attached to his mesh. The ChildActor is the ball, and it is placed inside the BallSocket of the skeletal mesh, located in the right hand.
By default, the character starts with a ball equipped. I throw it at the beginning of the game and then try to pick it up again by walking over the collision box I placed on the ball’s mesh. But I just can’t pick up the ball properly.

Just to confirm — the logic for picking up the ball should be coded in the ball’s blueprint, right?

I would recommend against using the Child Actor Component, it’s really buggy.
Instead use Spawn Actor from Class node to

  1. Spawn the Ball Actor in your Character’s Begin Play
  2. Store it in a variable.
  3. Use Attach Actor to Component to attach the Ball Actor to the Character’s mesh.

Also set collision for the Ball to No Collision

When throwing the ball, detach it, Enable Collision, Physics etc… then run the throwing code.


*quick low effort throwing code


Result

Thanks a lot ! I have severals questions

Why don’t you use a ProjectileMovement Component in the BP Ball instead of “Set Simulate Physics” and “Add Impulse” ?

Where do you set your collision profile name ? I mean what does “PhysicsActor” refers to?

But it will help a lot for my work !

I have to adapt the throwing part because I want to throw the ball where I click, in the throwing range of the character. It’s a top down view.

This was just what I thought of at the moment, The ProjectileMovement Component should probably be fine too. Though it’s bit harder to control the direction when using ProjectileMovement as you have to set the balls rotation correctly before throwing.

In the balls mesh’s details panel, there should be an option called Collision Presets. That’s where you initially set No Collision (so that the ball doesn’t interfere with the character while it’s in the hand)

In the Graph, there is no Set Collision Presets node, instead it’s called Set Collision Profile Name. If you click on the dropdown for Collision Presets in the Details Panel, it will display all the available options for different configurations of collisions. That’s where I got PhysicsActor from, which is mainly used for actor simulating physics.

I modified the project a little match the above description:


In case you didn’t know, you can right click the Get node for a reference variable and convert it to Validated Get Node to avoid using a separate Is Valid node.

code for GetMouseDirectionFromCharacter function from above

Result

This is still not perfect, throw direction will be less accurate as the mouse gets far from the character (For which I don’t have a solution right now, others might know a more accurate way.)

Project Files :

2 Likes

Thanks Dude you are awesome ! I will try this and share the result!

1 Like

Well i still have problems :

My character shouldn’t start with a ball in his hand. That’s why I didn’t use the ‘Spawn Ball’ part in the character’s Event Begin Play.. Instead, I used a component collision box in the BP_ball, and I used the ‘End Overlap’ event on the collision box to equip it to the character.

Once the ball is in the character’s hand, I need to be able to throw it into the blue area that appears after clicking the ‘ThrowBall’ button. The location where I click with the mouse is stored in a variable called ‘selected location’ in my PlayerController. If I click on one of my team’s characters, the character I clicked on is stored in a variable called ‘Selected Character’.
But with this setup, I can’t properly get a reference to the ball inside the character’s blueprint without using a Child Actor component or get a reference in the PlayerController . That’s why I can’t manage to code the throwing part. There can be only one ball in the game : being hold by a character or free on the groud.