• 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 Intensity3000 · Mar 05, 2013 at 03:26 AM · ai

How do you create a line of sight for AI?

I have tried several different ways to get the AI to "see" enemies in its line of view, but every attempt I have made so far has not worked out so very well. How do you create a line of sight for AI characters and have the script make the decision of which enemy is the greatest threat?

Comment
Add comment · Show 2
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 robertbu · Mar 05, 2013 at 05:24 AM 0
Share

Are there obstacles in the way? How do you define "see."

avatar image Intensity3000 · Mar 11, 2013 at 12:14 AM 0
Share

well by "see" i mean if the enemy (player) is within the bounds of vision, and there is nothing obstructing the AI's line of sight. and thank you very much hnsmith you were very helpful. I got an idea from what you said about raycasts and it worked! have the raycast co$$anonymous$$g from an empty transform that rotates right and resets when it reaches its limit, which seems to create a "field of vision"

1 Reply

· Add your reply
  • Sort: 
avatar image
8

Answer by hnsmith · Mar 05, 2013 at 05:07 AM

There are many different possible answers to this question, depending on your game type and how accurate you want the algorithm to be. I'll try and share a couple of ideas with you, but you'll have to make the ultimate decision what makes the most sense for your game.

Basic Test

solid first step is to perform a frustum test, this is basically checking to see if the player is even in the viewing plane of the AI (without testing for occlusion, yet). Unity supplies two very handy functions for doing this. http://docs.unity3d.com/Documentation/ScriptReference/GeometryUtility.CalculateFrustumPlanes.html http://docs.unity3d.com/Documentation/ScriptReference/GeometryUtility.TestPlanesAABB.html

Raycast Technique

f you determine that it's possible for the player to be visible, you have several options. I would probably use some series of raycasts based on the type of game.
http://docs.unity3d.com/Documentation/ScriptReference/30_search.html?q=raycast

Would just a raycast pointed at the head be sufficient? Or just a raycast pointed at the body? This would fail in cases where parts of the player stick out of cover, but for AI in a first person shooter, it might be okay to be a little spongey. It would be frustrating for the player (especially where they can't see their own bodies) if a finger, or spike of hair, or toe, stuck out and gave away their position. In general, for first person shooters I found that "I can't see you then you can't see me" approach works well. A single raycast right into the center of the camera does the trick 90% of the time.

For more accuracy I might try using multiple raycasts. Based on your animations and models, your cover, and your level geometry, determining what targets for your raycasts and how many hit positives determine a "sighting" need to be carefully considered. Head and body? Feet? Hands?

General Purpose Solution

f you need something accurate and all purpose you could try shooting 4 raycasts through the player collider at the top, bottom, and horizontal extremes. If 2 or more of these trigger, then you have a sighting. Attach the main camera to an AI and watch them navigate the world. Pay attention to situations you think should be a "hit" and add additional targets as necessary.

Performance Considerations

aving a few raycasts here may not be as computationally expensive as you might initially think (depending on the number of AI). Because we are first doing a frustum check so we don't likely need to perform this for every AI. We don't even necessarily have to calculate these every frame (only when the player or the AI moves). Even still, you probably don't have to perform them every frame even in motion. Maybe once every half a second would give a reasonable reaction time for a human walking down the hallway not really expecting to see anything. You can play with the timing to get the desired results.

If you have a LOT of AI, all in the same area, and ALL finding the player in their view frustum, you can do a round robin approach. Maybe every "look" update, only 25 AIs at a time actually perform the test. Even with 100 enemies all in a room at once, all with the player in their view frustum at all times but with only one AI who can actually see him it will only take 2 seconds worst case to spot him. In reality it would almost certainly be much shorter. You can improve these results by allowing everyone to do a "quick and dirty" check (a single raycast to center of mass to the player) while throttling the "deep look" (4+ raycasts).

Avoid Premature Optimization

wouldn't optimize with this until you need to though, for most use cases, I think you'll be fine. If you knew how many raycasts were being fired every frame by the zombies in Left 4 Dead, you'd be shocked that the game runs at all.

Food for Thought

Also remember that since it's AI, it's okay to let the player get away a little more with it. Interesting study, if you tell a person a game is stealth based, give them the right mood and setting, and give them the tools to be stealthy (crouching, walking, hiding in shadows), but make them completely invisible to the AI at all times, how long will the player think they are just successfully being stealthy before they realize the AI doesn't really work? How much more satisfying would that initial play session be if the AI was hyper realistic and spotting the player immediately when even slightly visible? I mean really, you just need them to get spotted enough to make it fun, which is probably very far from realistic (like most game mechanics).

Comment
Add comment · Show 3 · 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 robertbu · Mar 05, 2013 at 06:38 AM 0
Share

@hnsmith - some interesting info here. A couple of questions. Is using CalculateFrustumPlanes() and TestPlanesAABB() any different than checking the Renderer.isVisible flag? Also CalculateFrustumPlanes() uses the camera. Do you have a recommended method if the AI is third person? Use the distance and the angle to calc "vision"?

avatar image hnsmith · Mar 05, 2013 at 07:10 AM 0
Share

The IsVisible is called from the object, and returns true if ANY camera can possibly see it. This doesn't tell us whether a specific camera can possibly see an object (which we need in order to prevent making the raycasts.) I suspect that under the hood this is exactly what it's doing, iterating over every Camera object and testing for visibility, or something similar to that. (Which means IsVisible is probably a poor way to 'quickly check if i need to test at all') (I have no evidence to support this)

You're right, in that CalculateFrustumPlanes requires you to pass in a camera. Here is a page describing creating the planes yourself:

http://www.lighthouse3d.com/tutorials/view-frustum-culling/geometric-approach-extracting-the-planes/

You could attach cameras to your AI. I created a simple script that spawned a camera every second and watched the profiler. In a scene with a single cube and nothing else the framerate dropped below 60fps at around 1000~ cameras. It was not GPU related, but rather, CPU and $$anonymous$$emory consumption for the volume of GameObjects. This means at least in a static idle state the cameras don't have significant overhead, but you'll want to profile it yourself in your own situation if you choose to go down this path.

This probably won't work for all situtations, but it might work for some.

You could also do interesting things once you have a usable camera object attached to every AI...

Just a thought!

In a 3rd person game with basic s$$anonymous$$lth mechanics I would probably start with 5 raycasts (top, bottom, horizontal extremes, and center) and go from there.

avatar image robertbu · Mar 05, 2013 at 07:28 AM 0
Share

Thanks for the in-depth info.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

how to not repeat the same Random State in a State Machine 1 Answer

Simulating Mouse Input for AI 0 Answers

My NavMeshAgent doesn't move at all!,Why my NavMeshAgent doesn't move at all? 0 Answers

Enemy tank AI script TopDown 0 Answers

getting enemys to follow character. 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