private void DoAttack() { if (DEBUG) { Debug.Log("Doing attack"); } foreach (Collider other in hitbox.GetColliding()) { if (DEBUG) { Debug.Log("Found entity in collider"); } if (other.gameObject.GetComponent <MeleeAIEnemy>()) { if (DEBUG) { Debug.Log("enemy taking damage"); } MeleeAIEnemy enemyAI = other.gameObject.GetComponent <MeleeAIEnemy>(); Vector3 knockbackDir = (enemyAI.transform.position - this.transform.position).normalized; Debug.Log(knockbackDir.y); enemyAI.TakeDamage(PLAYER_DAMAGE, HIT_STRENGTH * knockbackDir); } } }
// Update is called once per frame void Update() { if (Time.time > nextSpawnTime) { nextSpawnTime = Time.time + coolDown; MeleeAIEnemy enemyAI = (MeleeAIEnemy)Instantiate(Resources.Load("Prefabs/Enemy", typeof(MeleeAIEnemy))); enemyAI.transform.position = transform.position; enemyAI.target = AIManager.Instance.playerAgent.transform; enemyAI.playerTrans = AIManager.Instance.playerAgent.transform; } }
//This gets called before Update functions. private void OnTriggerStay(Collider other) { if (other.gameObject.GetComponent <MeleeAIEnemy>() && Time.time > nextAttackTime) { //Attack enemy AI MeleeAIEnemy enemyAI = other.gameObject.GetComponent <MeleeAIEnemy>(); nextAttackTime = Time.time + Random.Range(0.5f, 1.0f); //nextAttackTime = Time.time + 0.5f; Vector3 temp = enemyAI.transform.position - this.transform.position; Vector3 knockbackDir = (new Vector3(temp.x, 0.0f, temp.z)).normalized; enemyAI.TakeDamage(1, HIT_STRENGTH * knockbackDir); } }
public void ResurrectEnemies() { Collider[] collidersInRange = Physics.OverlapSphere(transform.parent.position, radius, 1 << 11); foreach (Collider resurrecteeCollider in collidersInRange) { Transform parent = resurrecteeCollider.transform.parent; MeleeAIEnemy enemyAI = parent.gameObject.GetComponent <MeleeAIEnemy>(); if (enemyAI != null && enemyAI.IsDead()) { MeleeAIAlly allyAI = (MeleeAIAlly)Instantiate(Resources.Load("Prefabs/Ally", typeof(MeleeAIAlly))); allyAI.transform.position = parent.position; allyAI.transform.rotation = parent.rotation; allyAI.master = transform.parent; allyAI.target = transform.parent; Destroy(parent.gameObject); } } }
IEnumerator UpdateNearbyEnemies() { while (true) { nearbyEnemies = new List <MeleeAIEnemy>(); Collider[] collidersInRange = Physics.OverlapSphere(transform.position, 15.0f, 1 << 9); foreach (Collider enemyCollider in collidersInRange) { MeleeAIEnemy enemyAI = enemyCollider.gameObject.GetComponent <MeleeAIEnemy>(); if (!enemyAI.IsDead()) { nearbyEnemies.Add(enemyAI); } } if (nearbyEnemies.Count == 0) { state = AIState.Patrolling; target = wanderingTransform; } else { //TODO: make enemies that are already surrounded by allies less desirable Transform closestAgent = nearbyEnemies[0].transform; float minDistance = Vector3.Distance(closestAgent.position, transform.position); for (int i = 1; i < nearbyEnemies.Count; i++) { float dist = Vector3.Distance(nearbyEnemies[i].transform.position, transform.position); if (dist < minDistance) { closestAgent = nearbyEnemies[i].transform; minDistance = dist; } } state = AIState.AttackingEnemy; target = closestAgent; } yield return(new WaitForSeconds(0.1f)); } }