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

People who like this

0 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

  • Sort: 
avatar image

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
zarriel

People who like this

1 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Problems when i have multiple enemies on the same scene C# 1 Answer

Help with AI follow range 0 Answers

Damage trigger? 1 Answer

Unity2D Check which direction the enemy AI is heading? 1 Answer

i am having problems with my neural network (made from scratch),I am creating a neural network, however, whenever i start it. it crashes 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