diff --git a/Assets/Scripts/Pet/PetAttack.cs b/Assets/Scripts/Pet/PetAttack.cs index 9c1c2a8cb72c13f21e73846a4a34b44b33c2ed77..f463c70d3d295512a9eaa3525b148dea9ca2a666 100644 --- a/Assets/Scripts/Pet/PetAttack.cs +++ b/Assets/Scripts/Pet/PetAttack.cs @@ -1,32 +1,63 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +using UnityEngine.AI; public class PetAttack : MonoBehaviour { public int damage = 10; public int timeBetweenAttack = 10; - + public float range = 2f; + public float detectRadius = 2f; + + float timer; + PetHealth petHealth; Animator anim; + RaycastHit raycastHit; + int shootableMask; - void Start() + void Awake() { anim = GetComponent<Animator>(); + petHealth = GetComponent<PetHealth>(); + shootableMask = LayerMask.GetMask("Shootable"); } // Update is called once per frame void Update() { - + timer += Time.deltaTime; + + // Spherecast to detect enemy + if (Physics.SphereCast(transform.position, detectRadius, transform.forward * range, out raycastHit, range, shootableMask)) + { + // Face the hit enemy + Vector3 targetDir = raycastHit.transform.position - transform.position; + transform.rotation = Quaternion.LookRotation(targetDir); + + // Attack + EnemyHealth enemyHealth = raycastHit.collider.GetComponent<EnemyHealth>(); + if (enemyHealth != null) + { + AttackEnemy(enemyHealth); + } + } } - // TODO: use raycast instead of collider void OnTriggerEnter(Collider other) { EnemyHealth enemyHealth = other.GetComponent<EnemyHealth>(); - if (enemyHealth != null) + AttackEnemy(enemyHealth); + } + + public void AttackEnemy(EnemyHealth enemyHealth) + { + if (timer >= timeBetweenAttack && enemyHealth != null && petHealth.currentHealth > 0) { + // Reset timer + timer = 0f; + anim.SetTrigger("Attack"); //Lakukan Take Damage diff --git a/Assets/Scripts/Pet/PetAttackerMovement.cs b/Assets/Scripts/Pet/PetAttackerMovement.cs index b350f657ee8c1eb2b6f5fa79666b78caf3d09f63..45523f57d2f5a6c3839b02caaa33dec22622a327 100644 --- a/Assets/Scripts/Pet/PetAttackerMovement.cs +++ b/Assets/Scripts/Pet/PetAttackerMovement.cs @@ -1,9 +1,40 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +using UnityEngine.AI; +// When in pet detect enemy (shootable), pet will chase public class PetAttackerMovement : PetMovement { - // When in pet detect enemy (shootable), pet will chase + public float detectRadius = 4f; // radius to detect enemy that will be chased + public float range = 4f; + public float maxDistanceToEnemy = 2f; + RaycastHit raycastHit; + int shootableMask; + + private void Start() + { + shootableMask = LayerMask.GetMask("Shootable"); + } + + void Update() + { + if (Physics.SphereCast(transform.position, detectRadius, transform.forward * range, out raycastHit, range, shootableMask)) + { + // Follow enemy + nav.SetDestination(raycastHit.transform.position); + + // Get a random point within a sphere with radius equal to distance + Vector3 destination = Random.insideUnitSphere * maxDistanceToEnemy; + destination += transform.position; // Add the agent's current position + + // Find the closest point on the NavMesh to the destination + if (NavMesh.SamplePosition(destination, out NavMeshHit navMeshHit, maxDistanceToEnemy, NavMesh.AllAreas)) + { + // Set the agent's destination to the closest point on the NavMesh + nav.SetDestination(navMeshHit.position); + } + } + } }