Another way of doing the directional force

One of the things I was curious about was why we had to apply the force to every part of the body and why we used an explosion force. I imagined that the effect it would create would be uniform across the body, which could be correct if you have a grenade but probably not right if you just hit one targeted spot. If you hit the body, the legs and arms should trail along. I was curious if I could create that sort of effect.

Here’s my code:

A few things you also have to change (not shown in code below)

  • On the UnitRagdoll: Create a reference for ragdollMainRigidBody. Attach the “Spine_02” via an editor reference.
  • On the ShootAction. Modify the class to have a reference to the shoot point as Brian suggests here.
  • If you’ve done the above on ShootAction, then when you call targetUnit.Damage() add a parameter to send this shootPoint all the way along as below within the Unit code:
    public void Damage(int damageAmount, Transform damageSource)
    {
        healthSystem.Damage(damageAmount, damageSource);
    }

Once you’ve done the above you can modify the UnitRagdoll code as below.

public class UnitRagdoll : MonoBehaviour
{
    [SerializeField] private Transform ragdollRootBone;
    [SerializeField] private Rigidbody ragdollMainRigidbody;

    public void Setup(Transform originalRootBone, Transform damageSource)
    {
        MatchAllChildTransforms(originalRootBone, ragdollRootBone);
        
        Vector3 targetPoint = transform.position;
        targetPoint.y = damageSource.position.y;

        Vector3 forceVectorNormalized = (targetPoint - damageSource.position).normalized;
        Vector3 forceVector = forceVectorNormalized * 80f; // 80 units of force in direction from unit to target

        ragdollMainRigidbody.AddForce(forceVector, ForceMode.Impulse);
    }
// rest of code is unchanged
}

Other things to note: I haven’t profiled it but I think this code will be more performant as I’m only applying the force once (not once per RB). I don’t know how much this will actually save since Unity’s physics code will still have to run every frame afterwards for every RB. I’ve also avoided a GetComponent() call by passing the rigid body directly in the editor.

Minor Limitation: Spine_02 is not the perfect spot. It’s a bit low. But it was the only location on the torso that had a rigid body. If you pause it just right you can see the trail hitting the upper body but the body’s position looking like it was hit in the lower body,

Example of effect in action:

1 Like

Also something that helped me with making changes across the event handling code:

If you are using Visual Studio click on an event’s declaration. Then click Shift-F12 (Windows). This will show you all references (within your solution) to that event. Namely this is where you invoke it and where you are registering the listeners.

This addresses my question I initially posed here: Code Flow question - #7 by Cat_Hill It was closed so I couldn’t add to it.

1 Like

Privacy & Terms