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 /
  • Help Room /
avatar image
0
Question by b_schwartz · Aug 31, 2017 at 08:35 PM · aienemy aienemiesmultiple objects

Multiple AI enemies: Using OnTrigger for "line of sight" activates all enemies at once

I have AI enemies, which walk to the closest player target and attack, when you enter their sphere trigger collider. This works just fine, until multiple enemies are added.

I have an EnemySight script with the following code:

 private void OnTriggerStay(Collider other)
     {
 
         if (other.gameObject == player)
         {
             playerInSight = true;
         }
     }

And then after one more script which determines the animate state, the following code moves the enemy to the player:

 void Walk()
     {
         if (enemySight.playerOnRight == true && !facingRight)
         {
             Flip();
         }
         else if (enemySight.playerOnRight == false && facingRight)
         {
             Flip();
         }
 
         navMeshAgent.speed = enemySpeed;
         enemyCurrentSpeed = navMeshAgent.velocity.sqrMagnitude;
         navMeshAgent.SetDestination(enemySight.target.transform.position);
         navMeshAgent.updateRotation = false;
     }

When more than one enemy is in the scene, both enemies react to "PlayerInSIght". I assume I need to add enemies to a list, and have only the index of an OnTrigger... react, but I don't know how to go about that.

I'd be happy to post my full project if it would help.

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 ppg · Aug 31, 2017 at 08:46 PM 0
Share

try

 private bool playerInSight ;
 

 
 // ins$$anonymous$$d of
 
 public bool  playerInSight ;

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by b_schwartz · Sep 01, 2017 at 02:48 AM

Problem solved!

Static animation variables was the issue. Making them public has cluttered my editor a bit, but each enemy now acts individually.

Edit: Private works as well (duh). Editor decluttered.

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 ppg · Aug 31, 2017 at 08:40 PM

 this.playerInSight = true;

Comment
Add comment · Show 6 · 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 ppg · Aug 31, 2017 at 08:44 PM 0
Share

you don't necessarily need a list for each object to react individually so far each have their own script attached to the game object it should work your issue might be with something else

avatar image b_schwartz ppg · Sep 01, 2017 at 02:47 AM 0
Share

I fixed it :D

The issue wasn't any of this, it was simply that my animator state variables were static, ins$$anonymous$$d of public, so both enemies were trying to share the same animation.

avatar image ppg b_schwartz · Sep 02, 2017 at 06:54 PM 0
Share

O$$anonymous$$ thought it might have something to do with how the var are reference ie public /private but it was static.

avatar image b_schwartz · Aug 31, 2017 at 09:48 PM 0
Share

Hmm, well that solved part of the problem. I put the move code inside that conditional, and now the second enemy doesn't react at all, and the proper enemy animates to walk, but it doesn't move, which is what this code is actually supposed to execute.

I will keep playing, thanks!

avatar image ppg · Aug 31, 2017 at 10:04 PM 0
Share

O$$anonymous$$ keep in $$anonymous$$d if using trigger function you have less steps than a update functions that runs continuously CODES $$anonymous$$OST TI$$anonymous$$ES WILL REACT DIFFERENTLY IN THE TRIGGER FUNCTION BECAUSE ITS $$anonymous$$EANT TO RUN ONCE ON TRIGGER

make sure you use a private bool also try targeting the instance object with the (this.object) syntax

check other questions related to your issue i have answered a few here today CHEC$$anonymous$$ QUESTION LISTS FOR TODAY good luck

avatar image b_schwartz ppg · Sep 01, 2017 at 01:02 AM 0
Share

Thanks for the tips :) I have indeed found a few questions related to this, but I'll take another look. So now I'm thinking I probably don't need to bother with a list, and just get everything under a "this" statement. But I do get that Triggers only run when they're triggered.

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

158 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

Related Questions

How to set and change enemy path in real time in game ? 1 Answer

Raycast Enemy AI shooting script 1 Answer

Interesting Enemy AI issue 2 Answers

How can I make an enemy follow the player when the player is not looking at the enemy? 0 Answers

Making smart enemy AI using GOAP 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