Animation physics collision

Hello,

I made a punching animation and use it on a cube (movable), but the cube didn’t react when the hand hit it.

Why and how can I fix it this collision issue

You could have a collision component attached to the character’s hand that is used to detect when and where the punch connects, then use an Add Impulse At Location node on the hit Actor. Also make sure the actor you want to move has Simulate Physics enabled in its Details panel. Keep reading for a more detailed explanation on the punch impulse setup, but I’m going to warn you now, this will be a rather long read.

For this, I’m going to assume you’re using a humanoid character with the default Mannequin model, but it can apply to just about anything (even melee weapons!). To start with, you should add a Sphere Collision Component to your character and make the Mesh its parent. I named mine RightHand because it’s for the right hand punch (you should have one for both hands if both are involved in the animation). Next, in the Details panel for the sphere, I have the following settings:

230538-melee-punch-collider-settings.png

The location is offset by -10 on the X axis to move it more toward the center of the hand, as the root of the bone it’s attached it is the wrist on the UE4 Mannequin. Then, we rotate it by 180 degrees on the Z axis to make have the forward vector of the collider point to the front of the fist. This might differ for your skeleton, but you can check the forward vector by adding an Arrow Component to the collider and see which way it points. Rotate the collider until that arrow points to the front of the fist. We use this vector later to apply the hit impulse. For the socket, this depends on your mesh. You can either manually add a socket to the Skeleton asset, or you can just attach to an existing bone. I just chose a bone for simplicity. Next, I scaled the sphere down to about the size of the hand.

Now that we have the collision set up, we’re going to add the actual code for it. In the collider Details panel, scroll to the bottom and click the big green plus (+) next to On Component Hit. Do this for both fist components if you are using both. This will take us to the event graph and add the On Hit event for us to work with. We’re going to need three new variables as well, so add a boolean for each hand that will indicate if we are actively punching with that hand. In my case, I added one named bRightPunch to indicate whether the right hand is punching or not. This will allow us to check if we’re actively punching before we apply the impulse. Otherwise, anything that touches our hand, punching or not, will be pushed away. Our third variable will be a float that stores how much force is behind each punch, which I called PunchForce. I set this to 100 by default, but it’s entirely dependent on how hard you want to push things with each punch (and 100 may be too much, I’m mostly doing this from memory since I don’t have animations to test with).

It’s finally time to hook up our nodes, so here we go. Coming off the On Component Hit event, we want a Branch, where the condition is the related punching bool (i.e. for RightHand component, the condition is bRightPunch). We’ll leave the False branch disconnected, and only use the True branch. Next, drag off a node from the Other Comp output of the event and add an Add Impulse At Location node. Connect the True branch into this. Next we need the impulse force we’re adding to the hit component, so for that calculation, we need to use a Get Forward Vector node on the appropriate hand component, then multiply this vector by PunchForce. Forward vector is always the positive X direction of whatever component/actor you retrieve it from, and multiplying this by PunchForce means we apply that much force in the forward direction based on the fist’s orientation. So it should always push away from the fist. We plug the result of this calculation into the Impulse input of the Add Impulse At Location node. Finally, we need the location of the hit, so pull the Hit output of the event node off and create a Break Hit Result and pull the location off that and onto the Location input of the Add Impulse At Location node. We’re almost done now! Here’s the full node layout in case I missed anything.

Last but not least, we need to set the punching boolean to True when we start a punch, and False when we end one. This is more dependent on your setup for starting and ending the punch animation, but basically you want to set the punching boolean to True at the start of the animation and False at the end. If you have access to the end of the animation (such as an Anim Notify), you can have the punching bool = false attached to that. Otherwise, if I recall correctly, you can put a delay after the Play Animation node (I think it’s called as soon as the anim starts) and have the delay equal to the length of the anim (or up to whatever point you want the punch active, like the time it takes to fully extend the arm), then after the delay you set punching = false. Here’s an example setup using the delay method:

I’m pretty sure that’s all you need to add collision to your punches. I did do this from memory though, so I might have missed something. Let me know if you’re still having trouble with this. I hope this helps!

PS: Sorry about the giant wall of text, I was trying to be very detailed to make sure I didn’t miss anything.

PPS: If you also want to do damage with your punches, you can use the Other Actor node of the Component Hit event with an Apply Damage node, and then for any subclass of Actor you want to take damage, you just add the Event AnyDamage (or Point/Radial depending on which Apply Damage node you use) to their Event Graph and handle the damage there.

:o thank you but I decided to leave this for the future, it’s too complicated :confused:

Great tutorial, Thanks!

Thanks, for this geat tutorial, but I doubt that it would work this way with “hit”. Since it is a kinematic movement driven by an animation, the physics code would never solve for it (would simply have no knowledge of it) , therefore it never fires a hit. An overlap event could be reported but bit would not.

I wish it worked the way you described, that would make things much easier.

the only case I can imagine is if the hit actor, the box has physics enabled on it.

Now in this case of Blackouts objective, an overlap would suffice, but if you hit data, you would have to implement something extra (maybe line traces)