Not sure if this is how it should be adding directional ragdolling to the deaths, but here goes,
public event EventHandler<Transform> OnDeath;
public void Damage(int damageAmount, Transform damageTransform = null)
{
health -= damageAmount;
if(health < 0)
{
health = 0;
}
if(health == 0 )
{
Die(damageTransform);
}
}
private void Die(Transform damageTransform = null)
{
OnDeath?.Invoke(this, damageTransform);
}
Extended the damage and a onDeath methods to take an optional transform for the damage location, in this case it will be the Unit that has fired the shot (but in the future could be say a grenade exploding, exploding barrel, etc),
In the UnitRagdollSpawner passed through the damageLocation transform to the UnitRagdoll setup
private void HealthSystem_OnDeath(object sender, Transform e)
{
var ragdollTransform = Instantiate(ragdollPrefab,transform.position,transform.rotation);
var unitRagdoll = ragdollTransform.GetComponent<UnitRagdoll>();
unitRagdoll.Setup(originalRootBone, e);
}
Then with in the Setup in UnitRagdoll set up and if there isn’t a damage source location then run the code with the old logic, but if there is a damageTransform then
public void Setup(Transform originalRootBone, Transform damageSourceTransform = null)
{
MatchAllChildTransforms(originalRootBone, ragdollRootBone);
Debug.Log(damageSourceTransform);
if (damageSourceTransform == null)
{
ApplyExplosionToRagdoll(ragdollRootBone, 1000f, transform.position, 10f);
}
else
{
var dmgDirectionOffest = (damageSourceTransform.position - transform.position ).normalized;
var dmgDirection = transform.position + dmgDirectionOffest;
ApplyExplosionToRagdoll(ragdollRootBone, 1000f, dmgDirection, 10f);
}
}
Find the directional offset normalised,(stolen from the MoveAction) between the damageSource and the ragdoll position.
Then apply that offset to the ragdolls position to get the world coordinates for the explosive force.
Preshot
Death from x:3 z:3 ragdoll lands in x:4, z:4
2nd unit fires at x:3 z:3 and ragdoll lands in x:2 z:2
Is this the most efficient way of implementing this directional death, could this cause any issues further down the line with this approach?