From 5c968ff9e9a2f46136af1af1ff78757c1bb1119a Mon Sep 17 00:00:00 2001 From: billc27 <110593711+billc27@users.noreply.github.com> Date: Thu, 9 May 2024 07:32:50 +0700 Subject: [PATCH] feat: add mechanism to hit enemy pet --- Assets/Scripts/Weapons/DefaultGun.cs | 10 ++++++++++ Assets/Scripts/Weapons/Shotgun.cs | 10 ++++++++++ Assets/Scripts/Weapons/Sword.cs | 11 +++++++++++ 3 files changed, 31 insertions(+) diff --git a/Assets/Scripts/Weapons/DefaultGun.cs b/Assets/Scripts/Weapons/DefaultGun.cs index 051b609..dd275d1 100644 --- a/Assets/Scripts/Weapons/DefaultGun.cs +++ b/Assets/Scripts/Weapons/DefaultGun.cs @@ -57,6 +57,16 @@ public class DefaultGun : Weapons enemyHealth.TakeDamage(damagePerShot, shootHit.point); } + // Try and find an EnemyPetHealth script on the gameobject hit. + EnemyPetHealth enemyPetHealth = shootHit.collider.GetComponent<EnemyPetHealth>(); + + // If the EnemyPetHealth component exist... + if (enemyPetHealth != null) + { + // ... the enemy pet should take damage. + enemyPetHealth.TakeDamage(damagePerShot, shootHit.point); + } + // Set the second position of the line renderer to the point the raycast hit. gunLine.SetPosition(1, shootHit.point); } diff --git a/Assets/Scripts/Weapons/Shotgun.cs b/Assets/Scripts/Weapons/Shotgun.cs index 3c55626..923b41b 100644 --- a/Assets/Scripts/Weapons/Shotgun.cs +++ b/Assets/Scripts/Weapons/Shotgun.cs @@ -46,6 +46,16 @@ public class Shotgun : Weapons int finalDamage = Mathf.Max(10, Mathf.RoundToInt(damagePerShot - hit.distance/range*damagePerShot)); enemyHealth.TakeDamage(finalDamage, hit.point); } + + EnemyPetHealth enemyPetHealth = hit.collider.GetComponent<EnemyPetHealth>(); + // If the EnemyPetHealth component exist... + if (enemyPetHealth != null) + { + // ... the enemy pet should take damage. + // Damage is lower the farther the enemy pet is. + int finalDamage = Mathf.Max(10, Mathf.RoundToInt(damagePerShot - hit.distance / range * damagePerShot)); + enemyPetHealth.TakeDamage(finalDamage, hit.point); + } } } } diff --git a/Assets/Scripts/Weapons/Sword.cs b/Assets/Scripts/Weapons/Sword.cs index 1fe2136..4a3c4b2 100644 --- a/Assets/Scripts/Weapons/Sword.cs +++ b/Assets/Scripts/Weapons/Sword.cs @@ -53,6 +53,17 @@ public class Sword : Weapons // ... the enemy should take damage. enemyHealth.TakeDamage(damagePerShot, shootHit.point); } + + // Try and find an EnemyPetHealth script on the gameobject hit. + EnemyPetHealth enemyPetHealth = shootHit.collider.GetComponent<EnemyPetHealth>(); + + // If the EnemyPetHealth component exist... + if (enemyPetHealth != null) + { + // ... the enemy pet should take damage. + Debug.Log("SWORDD"); + enemyPetHealth.TakeDamage(damagePerShot, shootHit.point); + } } } } -- GitLab