Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by JannickL · Feb 23, 2017 at 09:26 PM · raycastenemy aioverlapfovchase

Check if player is in enemys fov?

Hey there, i'd like to let my enemy start chasing the player when he is in his fov (~180°). The enemy is flying btw.

I read about using Physics.OverlapSphere

Anyone got a solution for this? I don't want to use triggers because it really annoying adding all those triggers to my different kind of enemies.

My idea is making an 180° raycast on the ground (from the position of the enemy) and check if the player got hitted too! If true the enemy should directly face the player and send out a standard raycast to check if there is an object between both (the player is hidden).

But how to make a 180° hit check?

Greets

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 hexagonius · Feb 23, 2017 at 10:19 PM 0
Share

it's way easier since you're ai$$anonymous$$g for 180°. do enemy.transform.InverseTransformPoint(player.transform.position). if the resulting z value is larger than 0 the player is in front of the enemy.
It's basically how you see a position in another's view, like sitting in a class and calculating where the $$anonymous$$cher in once own relative space is.

1 Reply

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

Answer by ThisGuyThatsHere · Feb 23, 2017 at 10:27 PM

I would just get the vector from enemy's position to player's position (eV - pV, or reverse, always getting confused there) and then check angle: Vector3.Angle(deltaV, enemy.transform.forward);

And that's it, you've got the angle from enemy's view to perfect view at player.

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 Bunny83 · Feb 24, 2017 at 03:29 AM 3
Share

It's reverse ^^. A vector is like an arrow. You always do "Head $$anonymous$$us Tail" or "To $$anonymous$$us From". Since you want the vector from your enemy to your player you have to do "pV - eV".

avatar image JannickL · Feb 24, 2017 at 08:24 AM 2
Share

Yeah it's reverse. Vector3.angle works perfectly! Thank you :)

 Vector3 targetDir = player.position - transform.position;
 float angleToPlayer = (Vector3.Angle(targetDir, transform.forward));
 
 if (angleToPlayer >= -90 && angleToPlayer <= 90) // 180° FOV
       Debug.Log("Player in sight!");
             
avatar image Zitronekoma30 JannickL · Dec 23, 2019 at 08:06 AM 0
Share

idk why but this exact method doesn't work for me, I even copied your code here but it still won't work

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

90 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

Related Questions

get enemy to chase and shoot at player wherever hes facing. 0 Answers

Help with Enemy AI 1 Answer

Using FOV to target multiple enemies 1 Answer

AI Field of vision 1 Answer

Problems with enemy ai 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