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 AlejandroBoss · May 18, 2017 at 11:06 AM · timescripting beginnerattackinghealth-deduction

How can I make my enemy attack me every few seconds?

This is the script that I'm using for the enemy to "attack" my player. What it basically does is that the script looks for whatever collided with my player and checks if it is tagged Enemy, if it is then the player takes damage. The only problem is that the player only takes damage when the enemy enters the trigger. I would like it to be so while the enemy is inside the trigger, it can constantly damage the player every couple of seconds. Thanks for the help and I'll be clearer if needed. public const int maxHealth = 100;

 public int currentHealth = maxHealth;

 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.CompareTag("Enemy"))
     {
         currentHealth -= 10;
         if (currentHealth <= 0)
         {
             currentHealth = 0;
             Destroy(gameObject);
         }
     }
 }

 public void TakeDamage(int amount)
 {
     currentHealth -= amount;
     if (currentHealth <= 0)
     {
         currentHealth = 0;
         Destroy(gameObject);
     }
 }

}

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SohailBukhari · May 18, 2017 at 11:16 AM

use OnTriggerStay instead of OnTriggerEnter because OnTriggerStay is called almost all the frames for every Collider other that is touching the trigger.

This message is sent to the trigger and the collider that touches the trigger. Note that trigger events are only sent if one of the colliders also has a rigidbody attached.

 void OnTriggerStay(Collider other)
     {
         if (other.gameObject.CompareTag("Enemy"))
      {
          currentHealth -= 10;
          if (currentHealth <= 0)
          {
              currentHealth = 0;
              Destroy(gameObject);
          }
      }
     }

OnTriggerStay can be a co-routine, simply use the yield statement in the function.

You can call TakeDamage using Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds.

   InvokeRepeating("TakeDamage", 2.0f, 0.3f);

Comment
Add comment · Show 2 · 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 AlejandroBoss · May 18, 2017 at 04:11 PM 0
Share

So would I just change OnTriggerEnter to OnTriggerStay making sure that one of the colliders has a rigidbody. Also I'm not entirely sure what you mean by OnTriggerStay being a co-routine by using a yield statement. I don't think I was clear, but I am not the best with coding, but I am trying to learn as much as I can. If you could maybe elaborate a bit more on what you mean with some examples, that would be very helpful. But I shall try to do it by myself using what you told me. I will try this to see if it works so thanks.

avatar image SohailBukhari AlejandroBoss · May 20, 2017 at 07:04 AM 0
Share

Just try and if you stuck anywhere then post code and we will solve the problem, if you try yourself then you can learn fast and better.

avatar image
0

Answer by dashticle · May 19, 2017 at 02:03 PM

You should read up on coroutines here:

https://docs.unity3d.com/Manual/Coroutines.html

Or search them on the Learn portal. Basically you write a function that returns IEnumerator, and call it with StartCoroutine(yourCoroutine). By using the yield statement, it will get called every x seconds as long as you wish.

The link has som simple examples and a better explanation.

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
0

Answer by anilthapliyal8024 · May 22, 2017 at 04:32 PM

As far as I remember Coroutine doesn't work correctly if you write inside OnTriggerEnter().

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

6 People are following this question.

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

Related Questions

How to Make Health Decrease Over Time 2 Answers

Stop movement while attacking 1 Answer

How to make a dodge command/travel a certain distance in a forward direction based on current forward vector then stop? 0 Answers

Pause Time when certain level loaded? 3 Answers

Digital Clock: Time.time > 60 2 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