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 · Apr 30, 2018 at 12:29 PM · enemydelayattacking

I am trying to make my enemy AI have an attack delay

right now my enemy AI attacks the player when in range but when it attacks it doesn't have a delay I've tried many different things to find an answer but none seem to work can someone help? My code given is being called by the void OnTriggerStay so it attacks each time the player is in the collider is there a way to add an attack delay to the enemies attack with the code i have given?

void OnTriggerStay(Collider other) { if (other.gameObject == player) {
Attack();
} }

' private void Attack() //Call this in OnTriggerStay if the colliding gameObject is the player { anim.SetTrigger("Attack"); //trigger our animation

     if (playerHealth.currentHealth > 0) 
     {
         playerHealth.TakeDamage(attackDamage); //make player take damage    
        
     }

     if (Time.time > attackDelay)//Check if current game time is at or past the time we last set
     {
         attackDelay = Time.time + attackRate; //Set timer to current game time plus our delay.
     }
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by the_genius · Apr 30, 2018 at 01:00 PM

The last few lines of your code are changing the value of the 'attackDelay' variable but doing nothing with it that causes a delay. You need to remember the value of Time.time when the enemy last attacked (call it lastAttacked) and only attack again if Time.time has increased above lastAttacked+attackDelay.

 public float attackDelay = 100;
 float lastAttacked = -9999;
 
 void OnTriggerStay(Collider other) {
     if (other.gameObject == player) {
         Attack();
     }
 }
 
 private void Attack() { //Call this in OnTriggerStay if the colliding gameObject is the player
     if (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 · 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

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

82 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

Related Questions

Enemy AI problems 2 Answers

delay first spawn 2 Answers

How to make an enemy respawn? 0 Answers

I need help making my enemy AI have an attack delay 2 Answers

Shoot Bullet Delay Enemy 0 Answers


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