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 ilagef · Mar 20, 2020 at 07:50 PM · triggershootingeventfield of viewfield of vision

Trigger on entering field of view

Hey guys,

I want to make an AI that shoot at target if it within the field of view (360) example: alt text

on the basic method, I put a circle on the AI GameObject, with a collide trigger, so I could call a function that start the AI shooting.

I want to improve the function, So the AI would know if it can hit the target directly or not. in the example, the orange circle cannot be attacked, but if it would move to the red circle, the AI would start to attack.

Is there an implementation to do it without checking on every sence update, with Ray if the target within the field of view?

Thanks.

question.png (55.1 kB)
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 metalted · Mar 20, 2020 at 10:13 PM 0
Share

Yes there is and it's probably raycast. Is there a reason you dont want to use the Raycast? Its the perfect use case for this because you want to create some line of sight. And thisis basically what raycast is as well. I wouldn't worry to much about performance issues or anything if that's what you are worried about.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Zentiu · Mar 20, 2020 at 11:02 PM

If i were you i would give your AI another collider and set it to trigger. I would set this trigger to the range of what the AI should look for at max range. Then the AI will have a list of 'targets' that enter the collider and set a raycast. if there is something in front of them, ignore it. if the raycast returns the enemy that he has in his list then unleash hell. :P

 raycasthit hit;
 vector3 direction = transform.lookat(firstTargetEnteredTrigger.transform.position);
 
 if(physics.raycast(AI.transform.position, direction, AIRange, out hit))
 {
     if(hit.collider.gameObject.Tag == "enemy")
     {
          //Unleash Hell!
     }
 }

Don't forget to add:

 public void OnTriggerEnter(collision col)
 {
     if(col.gameObject.Tag == "Enemy")
     {
         //Create this list and when anyone enters that is an enemy, add it. Do NOT forget to remove them afterwards when the enemy dies or goes out of trigger like shown below.
         listOfEnemies.add(col.gameObject);

 
 }

 Public void OnTriggerExit(Collision col)
 {
     if(col.gameObject.Tag == "Enemy" && listOfEnemies.contains(col.GameObject))
     {
         listOfEnemies.remove(col.GameObject);    
     }
 }

You need to check every frame or if you set up a timer instead if the enemy changed position like 'no longer behind a wall or whatever' but at least you now have a list f enemies for this AI that happen to be in range and let the AI handle who to shoot for first in code. Just don't forget to create that 'listOfEnemies' list.

I hope this is helpful. If not, let me know.

Comment
Add comment · Show 1 · 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 ilagef · Mar 21, 2020 at 09:08 AM 0
Share

Hey Zentiu, first of all thank you for reply. You suggest to make a selective check on the enemies who are relevant. I will implement your suggestion with an interval of 60 checks per sec, If I won't find a more efficient solution.

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

151 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

Related Questions

How do I trigger an event (health loss) only once? 3 Answers

Trigger event by bringing controller to my Head,Swap to Camera Mode 0 Answers

Toggle onPointerDown event 2 Answers

Scoring System for Pinball 0 Answers

Is there an event/hook/trigger to sub to when a texture is applied to a gameObject in scene in editor mode? 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