Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by noahcscott1142000 · May 08, 2018 at 12:13 PM · enemydelayattack

I need help making my enemy AI have an attack delay

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<PlayerHealth>();
     //enemyHealth = GetComponent<EnemyHealth> ();
     anim = GetComponent<Animator>();
 }

 // 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;
     }
 }

}

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IvovdMarel · May 08, 2018 at 12:20 PM 0
Share

Please clarify your issue. This script could work if the player enter the Trigger attached to the Enemy (which would call OnTriggerStay).

avatar image noahcscott1142000 IvovdMarel · May 08, 2018 at 12:59 PM 0
Share

The issue right now is that when my player enters the collider the enemy attacks but it attacks really fast and my health drops from 100 to 0 pretty much instantly im wondering how i would make the delay be longer so i dont die instantly

avatar image noahcscott1142000 · May 08, 2018 at 01:49 PM 0
Share

$$anonymous$$y problem is that when the player enters the collider the enemy attacks but when it attacks its really fast and my player health drops from 100 to 0 and the player dies instantly with any chance is there some way to make it so the the time between the attacks or damage dealt to the player is takes longer to kill the player would i have to make the damage dealt really small to where it's a decimal or is the some way to make it so the player takes the damage but at a slower rate

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by tormentoarmagedoom · May 08, 2018 at 04:00 PM

Good day.

You need to create a cooldown timer, and atack only if cooldown has reached 0.

something like this...

 float TimerForNextAttack, Cooldown;
 
 vois Start()
 {
 Cooldown = 3;
 TimerForNextAttack = Cooldown;
 
 void Update()
 {
  if (TimerForNextAttack > 0)
  {
  TimerForNextAttack  -= Time.deltaTime;
  }
  else if (TimerForNextAttack <=0)
   {
   Atack();
   TimerForNextAttack = Cooldown;
   }
 }


Bye!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by anthot4 · May 08, 2018 at 01:00 PM

On Line 15 you could have:

Invoke("Attack",2.0f); // instead of just calling attack. This calls the attack function in 2 seconds.

Or if you want something more random:

var Delay = Random.Range(1,3); // choose a number between 1 and 3 to wait for the enemy to attack

Invoke("Attack",Delay);

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image noahcscott1142000 · May 08, 2018 at 01:53 PM 0
Share

thx for this it helped with the initial delay before the attack but what im more trying to get is the delay between each attack when the player has damage being dealt to them because when the player is in range it starts to attack but it attacks at a really fast rate is there some way to make the delay between attacks longer as well

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

85 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating basic enemy animation and attacking 4 Answers

Multiple Enemy Collision Help 0 Answers

How to make an enemy attack every few seconds 1 Answer

Enemy AI problems 2 Answers

Problem with enemy AI 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges