Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

I need help making my enemy AI have an attack delay

$
0
0
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class EnemyAttack : MonoBehaviour { public int nulldamage = 0; public float attackDelay = 10; float lastAttacked = -999; public int attackDamage = 2; Animator anim; GameObject player; PlayerHealth playerHealth; //float timer; //public float attackRate = 0f; //EnemyHealth enemyHealth; // Use this for initialization void Awake() { player = GameObject.FindGameObjectWithTag("Player"); playerHealth = player.GetComponent(); //enemyHealth = GetComponent (); anim = GetComponent(); } // Update is called once per frame void OnTriggerStay(Collider other) { if (other.gameObject == player) { Attack(); } } void Update() { if (playerHealth.currentHealth <= 0) { Destroy(player); anim.SetTrigger("PlayerDead"); } } private void Attack() //Call this in OnTriggerStay if the colliding gameObject is the player { while (Time.time > lastAttacked + attackDelay) { anim.SetTrigger("Attack"); //trigger our animation if (playerHealth.currentHealth > 0) { playerHealth.TakeDamage(attackDamage); //make player take damage } lastAttacked = Time.time; } } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>