From c2d32524b388244cc85e622a5a96ef0ef829fb4c Mon Sep 17 00:00:00 2001 From: dethaa <sharonmarbun12@gmail.com> Date: Sun, 10 Apr 2022 15:36:26 +0700 Subject: [PATCH] fix(skeleton shooting): only shoot when player is in range --- Assets/Scripts/Enemy/SkeletonShooting.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/Enemy/SkeletonShooting.cs b/Assets/Scripts/Enemy/SkeletonShooting.cs index 682d821..e4662ee 100644 --- a/Assets/Scripts/Enemy/SkeletonShooting.cs +++ b/Assets/Scripts/Enemy/SkeletonShooting.cs @@ -4,12 +4,14 @@ using UnityEngine; public class SkeletonShooting : MonoBehaviour { - Transform player; + // Transform player; + GameObject player; PlayerHealth playerHealth; public EnemyHealth enemyHealth; public int damagePerShot = 20; public float timeBetweenBullets = 3f; public float range = 100f; + public float shootingRange = 20f; float timer; Ray shootRay = new Ray(); //* @@ -28,7 +30,7 @@ public class SkeletonShooting : MonoBehaviour gunLine = GetComponent<LineRenderer>(); gunAudio = GetComponent<AudioSource>(); gunLight = GetComponent<Light>(); - player = GameObject.FindGameObjectWithTag("Player").transform; + player = GameObject.FindGameObjectWithTag("Player"); playerHealth = player.GetComponent<PlayerHealth>(); } @@ -40,12 +42,23 @@ public class SkeletonShooting : MonoBehaviour { DisableEffects(); } - if(timer >= timeBetweenBullets && Time.timeScale != 0 && enemyHealth.currentHealth >0 && playerHealth.currentHealth >0) + if(PlayerInRange() && timer >= timeBetweenBullets && Time.timeScale != 0 && enemyHealth.currentHealth >0 && playerHealth.currentHealth >0) { Shoot(); } } + bool PlayerInRange() + { + float distance = Vector3.Distance(transform.position, player.transform.position); + if (distance <= shootingRange){ + return true; + } else { + return false; + } + + } + public void DisableEffects() { gunLine.enabled = false; -- GitLab