//敵を引き付けるそしてタイマーに合わせてダメージをつける private void Update() { damageCounter -= Time.deltaTime; attractTime -= Time.deltaTime; if (damageCounter <= 0) { damageCounter = damageTime; //raycast で敵hpにアクセスする Collider[] allEnemies = Physics.OverlapSphere(transform.position, 5); foreach (var e in allEnemies) { SV_EnemyHealth h = e.GetComponent <SV_EnemyHealth>(); if (h != null) { h.takeDamage(1); } } } //引き付ける if (attractTime <= 0) { attractTime = attractTimeDelay; SV_EnemyController[] controller = FindObjectsOfType <SV_EnemyController>(); foreach (var e in controller) { if (e != null) { e.changeTarget(this.transform); } } } }
NavMeshAgent nav; // Reference to the nav mesh agent. void Awake() { // Set up the references. player = GameObject.FindGameObjectWithTag("Player").transform; playerHealth = player.GetComponent <SV_PlayerHealth>(); enemyHealth = GetComponent <SV_EnemyHealth>(); nav = GetComponent <NavMeshAgent>(); }
float timer; // Timer for counting up to the next attack. void Awake() { // Setting up the references. player = GameObject.FindGameObjectWithTag("Player"); playerHealth = player.GetComponent <SV_PlayerHealth>(); enemyHealth = GetComponent <SV_EnemyHealth>(); anim = GetComponent <Animator>(); }
public void Shoot() { // Reset the timer. timer = 0f; // Play the gun shot audioclip. // gunAudio.Play(); // Enable the light. gunLight.enabled = true; // Enable the line renderer and set it's first position to be the end of the gun. gunLine.enabled = true; gunLine.SetPosition(0, transform.position); // Set the shootRay so that it starts at the end of the gun and points forward from the barrel. shootRay.origin = transform.position; shootRay.direction = transform.forward; // Perform the raycast against gameobjects on the shootable layer and if it hits something... if (Physics.Raycast(shootRay, out shootHit, range, shootableMask)) { // Try and find an EnemyHealth script on the gameobject hit. SV_EnemyHealth enemyHealth = shootHit.collider.GetComponent <SV_EnemyHealth>(); // If the EnemyHealth component exist... if (enemyHealth != null) { // ... the enemy should take damage. enemyHealth.TakeDamage(damagePerShot, shootHit.point); } // Set the second position of the line renderer to the point the raycast hit. gunLine.SetPosition(1, shootHit.point); } // If the raycast didn't hit anything on the shootable layer... else { // ... set the second position of the line renderer to the fullest extent of the gun's range. gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range); } }