Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 bulldogpancake · Apr 07, 2021 at 05:48 AM · triggeraiai problems

OnTriggerStay only applying to one object

Im working on a AI combat system where each AI has a secondary collider with "Trigger" enabled. here is my script so far

    public float health = 100;
     public int isrunning = 1;
     public GameObject currenttarget;
     public int attackspeed;
     public int damage;
     public int newdamage = 0;
     void Start()
     {
         StartCoroutine(DoDamage());
         
     }
 
     public void TakeDamage(float x)
     {
         this.health = this.health - x;
     }
 
     public IEnumerator DoDamage()
     {
         isrunning = 0;
         yield return new WaitForSeconds(attackspeed);
         Debug.Log("loop");
         newdamage = damage;
         isrunning = 1;
     }
 
 
 
     private void OnTriggerStay(Collider other)
     {
         if ( other.gameObject.CompareTag("AI"))
             {
             other.GetComponent<Framework>().TakeDamage(newdamage);
             newdamage = 0;
         }
 
     }
 
     private void Update()
     {
         if (isrunning==1)
         {
             StartCoroutine(DoDamage());
         }
     }
 
 
     // Update is called once per frame
 
 }
 

When I place three objects with this script where there damage is set to 5 and attack rate to 1, The result that I want out of this would be: A.100 B.100 C.100 (1 Second) A.80 B.80 C.80 However what I find is that the other.GetComponent().TakeDamage is only applying to one object at a time rather then being applied to the other two objects as I want. is this how the OnTriggerStay should be working? and if so are there any workarounds for this?

Comment
Add comment · Show 1
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 logicandchaos · Apr 07, 2021 at 02:51 PM 0
Share

can you post your scene and inspector? Check your tags. OnTriggerStay is very heavy as well, you might want to use OnTriggerEnter and OnTriggerExit and keep track with a bool.

  private void OnTriggerStay(Collider other)
  {
      if ( other.gameObject.CompareTag("AI"))
          {
          other.GetComponent<Framework>().isTakingDamage=true;
      }
  }

Then in your Framework class you have in update

 if(isTakingDamage)
      TakeDamage();

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by TheBestTroll · Apr 07, 2021 at 08:02 PM

well. I think if you're placing various colliders at the same time, only one of them will activate the trigger. I think the solution is to write:

 void OnTriggerStay(Collider[] cols){
 
  foreach(Collider c in cols){

   if(c.tag == "AI"){

   // do damage stuff

   }

  }
 
 }

This way, every frame a collider is TOUCHING the trigger, it's gonna be detected, and the damage will be applied.

But this is not really good, at all. I think the best solution would be to create a array of colliders (where you're going to place all the colliders that touch the trigger), then, using OnTriggerEnter, add them to the array, and remove them by OnTriggerExit. So you can use a foreach every time you want to apply damage to the colliders. This is a way better solution, since the performance will be optimized.

If you don't know what's a foreach or any other thing I said here, would be really good if you search and learn by yourself. It's way easier to learn when you know exactly what you're learning, and why.

I'm sorry for my bad english! Hope I helped you someway.

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

211 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 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 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

Collision with instantiated non-moving objects. 1 Answer

Another function for activating a different script 0 Answers

Resize Array Based on Value 2 Answers

Thief Car AI for 3D Game 0 Answers

Disable a target after trigger exit? 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