From f2c9e5ff75634f8d1f2b1a89b02e478289407ca8 Mon Sep 17 00:00:00 2001 From: in0finite Date: Sun, 17 May 2020 01:50:30 +0200 Subject: [PATCH] inflict damage based on part of body that was hit --- Assets/Prefabs/Ped.prefab | 4 +-- Assets/Scripts/Behaviours/Damageable.cs | 1 + Assets/Scripts/Behaviours/Ped/Ped_Damage.cs | 15 ++++++++ Assets/Scripts/Behaviours/PedManager.cs | 6 ++++ Assets/Scripts/Behaviours/PedModel.cs | 40 ++++++++++++++++++++- Assets/Scripts/Behaviours/Weapon.cs | 2 +- 6 files changed, 64 insertions(+), 4 deletions(-) diff --git a/Assets/Prefabs/Ped.prefab b/Assets/Prefabs/Ped.prefab index 7b575b65..e8a4ac80 100644 --- a/Assets/Prefabs/Ped.prefab +++ b/Assets/Prefabs/Ped.prefab @@ -448,8 +448,8 @@ MonoBehaviour: m_onDamage: m_PersistentCalls: m_Calls: - - m_Target: {fileID: 114220037769542022} - m_MethodName: HandleDamageByDefault + - m_Target: {fileID: 114099438993920694} + m_MethodName: OnDamaged m_Mode: 1 m_Arguments: m_ObjectArgument: {fileID: 0} diff --git a/Assets/Scripts/Behaviours/Damageable.cs b/Assets/Scripts/Behaviours/Damageable.cs index 4571081c..8a7959b3 100644 --- a/Assets/Scripts/Behaviours/Damageable.cs +++ b/Assets/Scripts/Behaviours/Damageable.cs @@ -8,6 +8,7 @@ namespace SanAndreasUnity.Behaviours public class DamageInfo { public float amount = 0f; + public Transform raycastHitTransform = null; public object data = null; } diff --git a/Assets/Scripts/Behaviours/Ped/Ped_Damage.cs b/Assets/Scripts/Behaviours/Ped/Ped_Damage.cs index 3657e51f..f2ee13fc 100644 --- a/Assets/Scripts/Behaviours/Ped/Ped_Damage.cs +++ b/Assets/Scripts/Behaviours/Ped/Ped_Damage.cs @@ -87,6 +87,21 @@ namespace SanAndreasUnity.Behaviours 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); + } + + } + } diff --git a/Assets/Scripts/Behaviours/PedManager.cs b/Assets/Scripts/Behaviours/PedManager.cs index 9b25d2ae..b9c3738e 100644 --- a/Assets/Scripts/Behaviours/PedManager.cs +++ b/Assets/Scripts/Behaviours/PedManager.cs @@ -17,6 +17,12 @@ namespace SanAndreasUnity.Behaviours public float minCameraDistanceFromPed = 2f; public float maxCameraDistanceFromPed = 30f; + [Header("Damage")] + + public float legAndArmDamageMultiplier = 0.8f; + public float stomachAndChestDamageMultiplier = 1.0f; + public float headDamageMultiplier = 4.0f; + [Header("Health bar")] public bool displayHealthBarAbovePeds = false; diff --git a/Assets/Scripts/Behaviours/PedModel.cs b/Assets/Scripts/Behaviours/PedModel.cs index 3d45054c..0ee55589 100644 --- a/Assets/Scripts/Behaviours/PedModel.cs +++ b/Assets/Scripts/Behaviours/PedModel.cs @@ -98,6 +98,8 @@ namespace SanAndreasUnity.Behaviours public Transform Pelvis { get; private set; } + readonly Dictionary m_damageLevelPerBones = new Dictionary(); + public class FrameAnimData { public Vector3 pos; @@ -327,7 +329,8 @@ namespace SanAndreasUnity.Behaviours void SetupRagdoll() { - // maybe assign forward vector to be the same as Z axis ? + + m_damageLevelPerBones.Clear(); RagdollBuilder rb = new RagdollBuilder { @@ -353,6 +356,41 @@ namespace SanAndreasUnity.Behaviours // set layer 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; } /// diff --git a/Assets/Scripts/Behaviours/Weapon.cs b/Assets/Scripts/Behaviours/Weapon.cs index 1e44321b..00a9c24e 100644 --- a/Assets/Scripts/Behaviours/Weapon.cs +++ b/Assets/Scripts/Behaviours/Weapon.cs @@ -635,7 +635,7 @@ namespace SanAndreasUnity.Behaviours } else { - damageable.Damage( new DamageInfo() { amount = this.Damage } ); + damageable.Damage( new DamageInfo() { amount = this.Damage, raycastHitTransform = hit.transform } ); } }