diff --git a/Assets/HUDManager.cs b/Assets/HUDManager.cs index f45d48b765ce2ebe4a125696dfe0d514ed72450f..357b0b65f9794791e5584ce860172b780c1d6573 100644 --- a/Assets/HUDManager.cs +++ b/Assets/HUDManager.cs @@ -107,25 +107,9 @@ public class HUDManager : MonoBehaviour } } - public void NewWeapon(string name) + public void NewWeapon(int id) { - int weaponSlot=0; - - if (name == "shotgun") - { - weaponSlot = 1; - } - - if (name == "sword") - { - weaponSlot = 2; - } - if (name == "bow") - { - weaponSlot = 3; - } - - GameObject slot = weaponsUISlot.transform.GetChild(weaponSlot).gameObject; + GameObject slot = weaponsUISlot.transform.GetChild(id-1).gameObject; slot.SetActive(true); } } diff --git a/Assets/Scenes/Level_01.unity b/Assets/Scenes/Level_01.unity index 335c7789b001269b50c6a90232eae400622d0747..af8283653567709e3df183c56f67cde164e8b669 100644 --- a/Assets/Scenes/Level_01.unity +++ b/Assets/Scenes/Level_01.unity @@ -11599,6 +11599,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: coins: 50 + coinsLatest: 0 currentPet: name: buffType: @@ -11609,12 +11610,12 @@ MonoBehaviour: isPetAlive: 0 isShopOpen: 0 isShopping: 0 - isEnemySpawning: 1 + isEnemyActive: 1 petFactory: {fileID: 734232483} currentWeapon: 0 availableWeapon: 01000000 hudManager: {fileID: 491809539} - enemyManager: {fileID: 164778954} + enemySpawner: {fileID: 0} --- !u!114 &1644740160 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 6b41088dd55acd5638c429780e2be8d081e1d5e4..385cfacd16361c95b0cb23c0b40996f3359198ff 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -90,7 +90,7 @@ public class GameManager : MonoBehaviour public void NewWeapon(int id) { - hudManager.NewWeapon(name); + hudManager.NewWeapon(id); availableWeapon[id - 1] = true; } 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); + } + } + } } diff --git a/ProjectSettings/TagManager.asset b/ProjectSettings/TagManager.asset index 789ae9d5049f4a5530c955edb496524a857a7576..9181309af82abb721224507a55f1618b094f200f 100644 --- a/ProjectSettings/TagManager.asset +++ b/ProjectSettings/TagManager.asset @@ -3,7 +3,8 @@ --- !u!78 &1 TagManager: serializedVersion: 2 - tags: [] + tags: + - Pet layers: - Default - TransparentFX