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 Inan-Evin · Mar 02, 2014 at 08:10 PM · anglelinesightofvector3.angle

Achieving 80 degreed line of sight using Vector3.Angle, and detecting if there is something in the way ?

Hello everybody, I'm writing some enemy cube AI and I'm kinda stuck somewhere. As everyone would wanted I wanted my AI to detect player if player is in detection range, also if AI is facing player, so we can sneak behind the AI. I achieved this by using Vector3.Angle, not raycasting directly because casting a ray from enemy's forward to check player, was not a very flexible way. Everything works okay, but now I'm stuck about detecting if something is between AI and player, when using Vector3.Angle, see my code here :

 var rayDirection = player.position - transform.position;
 
      if(Vector3.Angle(rayDirection, transform.forward) < angleToDetect);
        {
             // detection functions
        }

so as can be seen above, AI has a variable called angleToDetect which is 80 now, meaning the AI has a "cone" line of sight. But now how can I check if there is something between AI and player in this 80 degreed line of sight, if I use raycast, I can only use forward direction to check, and it will be very silly since I want my enemy to have cone line of sight. Any ideas about how to check if there is something on the 80 degreed line of sight between player and AI ? Thanks :)

Comment
Add comment
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

1 Reply

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

Answer by fafase · Mar 02, 2014 at 08:23 PM

You can use raycast or linecast, linecast is just easier. Place an object at the level of the eyes of your AI guy (it makes more sense to see from the eyes than the position which is in the middle), then cast a line from this point to the player position (or an object attached to the player at head level)

 if(!Physics.Linecast(eyes.position, playerHead.position, layer)){// You see each other}

Place the player and AI in the layer so that they are not considered in the method. This will allow the AI to see the player even though he is looking away from the AI.

EDIT: About layer: First add a layer at Layer 8, call it LOS and assign your player and AI. Now we need to consider all but this layer so in code:

 // This set the bit 8 to 1
 int layerMask = 1 << 8;
 void Start(){
     // This inverts them all
     // bit 8 is now the only 0
     layerMask = ~layerMask;
 }
 
 void Update(){  
     if (!Physics.Linecast (eyes.position, eyePlayer.position, layerMask)) {
           // See the player
     }
 }

More about this here: http://unitygems.com/basic-ai-character/#Sight

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 Inan-Evin · Mar 02, 2014 at 08:52 PM 0
Share

I did what you say, I added a wall between them but AI still sees the player and attacks, maybe because I just didnt completely understand the layer thing, I added them both on same layer, which I created layer 8, can there be anything that I need to give attention on the scene ?

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

20 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

Related Questions

Line of sight audio 2 Answers

Adding a line of sight? 1 Answer

Raycast target line of sight 1 Answer

what is wrong with my Vector3.Angle() code? 1 Answer

What is the 3rd point of Vector3.Angle ? 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