inflict damage based on part of body that was hit

This commit is contained in:
in0finite 2020-05-17 01:50:30 +02:00
parent b203a0b667
commit f2c9e5ff75
6 changed files with 64 additions and 4 deletions

View file

@ -448,8 +448,8 @@ MonoBehaviour:
m_onDamage: m_onDamage:
m_PersistentCalls: m_PersistentCalls:
m_Calls: m_Calls:
- m_Target: {fileID: 114220037769542022} - m_Target: {fileID: 114099438993920694}
m_MethodName: HandleDamageByDefault m_MethodName: OnDamaged
m_Mode: 1 m_Mode: 1
m_Arguments: m_Arguments:
m_ObjectArgument: {fileID: 0} m_ObjectArgument: {fileID: 0}

View file

@ -8,6 +8,7 @@ namespace SanAndreasUnity.Behaviours
public class DamageInfo public class DamageInfo
{ {
public float amount = 0f; public float amount = 0f;
public Transform raycastHitTransform = null;
public object data = null; public object data = null;
} }

View file

@ -87,6 +87,21 @@ namespace SanAndreasUnity.Behaviours
return pos; return pos;
} }
public void OnDamaged()
{
DamageInfo damageInfo = this.Damageable.LastDamageInfo;
float amount = this.PlayerModel.GetAmountOfDamageForBone(damageInfo.raycastHitTransform, damageInfo.amount);
this.Health -= amount;
if (this.Health <= 0)
{
Object.Destroy(this.gameObject);
}
}
} }

View file

@ -17,6 +17,12 @@ namespace SanAndreasUnity.Behaviours
public float minCameraDistanceFromPed = 2f; public float minCameraDistanceFromPed = 2f;
public float maxCameraDistanceFromPed = 30f; public float maxCameraDistanceFromPed = 30f;
[Header("Damage")]
public float legAndArmDamageMultiplier = 0.8f;
public float stomachAndChestDamageMultiplier = 1.0f;
public float headDamageMultiplier = 4.0f;
[Header("Health bar")] [Header("Health bar")]
public bool displayHealthBarAbovePeds = false; public bool displayHealthBarAbovePeds = false;

View file

@ -98,6 +98,8 @@ namespace SanAndreasUnity.Behaviours
public Transform Pelvis { get; private set; } public Transform Pelvis { get; private set; }
readonly Dictionary<Transform, int> m_damageLevelPerBones = new Dictionary<Transform, int>();
public class FrameAnimData public class FrameAnimData
{ {
public Vector3 pos; public Vector3 pos;
@ -327,7 +329,8 @@ namespace SanAndreasUnity.Behaviours
void SetupRagdoll() void SetupRagdoll()
{ {
// maybe assign forward vector to be the same as Z axis ?
m_damageLevelPerBones.Clear();
RagdollBuilder rb = new RagdollBuilder RagdollBuilder rb = new RagdollBuilder
{ {
@ -353,6 +356,41 @@ namespace SanAndreasUnity.Behaviours
// set layer // set layer
this.RootFrame.gameObject.SetLayerRecursive(Ped.PedBoneLayerName); this.RootFrame.gameObject.SetLayerRecursive(Ped.PedBoneLayerName);
// assign damage level for bones
Transform[] stomachAndChest = new Transform[] { this.RootFrame.transform, this.UpperSpine.transform };
Transform[] legsAndHands = new Transform[] { this.L_Thigh, this.R_Thigh, this.L_Calf, this.R_Calf,
this.L_Foot, this.R_Foot, this.LeftUpperArm, this.RightUpperArm, this.LeftForeArm, this.RightForeArm };
Transform[] headAndJaw = new Transform[] { this.Head.transform, this.Jaw.transform };
foreach (var bone in stomachAndChest)
{
m_damageLevelPerBones[bone] = 2;
}
foreach (var bone in legsAndHands)
{
m_damageLevelPerBones[bone] = 1;
}
foreach (var bone in headAndJaw)
{
m_damageLevelPerBones[bone] = 3;
}
}
public float GetAmountOfDamageForBone(Transform boneTransform, float baseDamageValue)
{
if (m_damageLevelPerBones.TryGetValue(boneTransform, out int damageLevel))
{
if (1 == damageLevel)
return baseDamageValue * PedManager.Instance.legAndArmDamageMultiplier;
if (2 == damageLevel)
return baseDamageValue * PedManager.Instance.stomachAndChestDamageMultiplier;
if (3 == damageLevel)
return baseDamageValue * PedManager.Instance.headDamageMultiplier;
}
return 0f;
} }
/// <summary> /// <summary>

View file

@ -635,7 +635,7 @@ namespace SanAndreasUnity.Behaviours
} }
else else
{ {
damageable.Damage( new DamageInfo() { amount = this.Damage } ); damageable.Damage( new DamageInfo() { amount = this.Damage, raycastHitTransform = hit.transform } );
} }
} }