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 masterdam17 · Nov 25, 2011 at 03:33 PM · enemyfunctionfovrepeatfield of view

Field of view for an enemy

Im trying to make a metal gear style project, and ive got my enemies arround waypoints, they rotate and face each one while they walk, pretty neat and its working just fine no errors nothing... since i need them to be able to spot you when you cross their field of view i need help to make them have one ... sooo ? what should i create to be used as a field of view... im quite new to programming so if you could please explain carefully i will appreciate that , thanks Also i need to make a function repeat until the transform.position of the object is equal to another... is there any way to do that? i tried do while and everything i tested out with that just crashed or freezed the game...

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Graham-Dunnett · Nov 25, 2011 at 03:50 PM

The average human has a FOV of approximately 160degrees.

Regarding the repeating until the positions match... Just compute a vector that moves the one point closer to the other at each frame. So, if one point was A=(0,0,0), and B=(10,0.0), and you want A to get to B in one second, then you need a small offet vector a=(1/fps*10). Just add a to A each frame and A will get closer to B.

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 masterdam17 · Nov 25, 2011 at 05:33 PM 0
Share

thanks... but you know , im new to this so if you could please write a simple script on how to do this it would really help me a lot, and also how would the vector detect collision with the player?

avatar image FLASHDENMARK · Nov 25, 2011 at 08:17 PM 1
Share

AIs are a very complex network of complexity. Asking for a script(and for free even) is most likely yielding you no results. You may do this on your own, but if you need help you are always welcome here, but this is no the place to ask for scripts.

avatar image masterdam17 · Nov 25, 2011 at 11:43 PM 0
Share

no im not asking actually a script to go copy paste it ... i mean just a piece of code or any other alternative i can have , or a better explanation in lets say "beginner programmer" language .

avatar image
0

Answer by aldonaletto · Nov 26, 2011 at 02:02 AM

You can't rely on the position of one object being equal to any specific position - there are too many ways for this to go wrong: small height differences, small direction misalignment, floating point inaccuracies etc. Usually you should find the vector object->target, clear the y component to ignore height differences and compare its magnitude to some small distance value:

  var vec = target.position - transform.position;
  vec.y = 0;
  if (vec.magnitude 
About the field of view, you should use a vector too: calculate the vector enemy->target, then get the angle between it and the enemy forward direction: if it's less than half the angle of view, the target is visible:

  var targetDir = target.position - transform.position;
  var angle = Vector3.Angle(targetDir, transform.forward);
  if (angle 

                    
Comment
Add comment · Show 4 · 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 masterdam17 · Nov 26, 2011 at 04:06 PM 0
Share

Niiiice im testing it out , thanks for the help man ill tell you then if it worked, but it seems logical so it must work :) thank you sir

avatar image masterdam17 · Nov 26, 2011 at 04:46 PM 0
Share

I GOT IT THAN$$anonymous$$S!! now i just need to check wether its behind a wall ... oh no :(

avatar image masterdam17 · Nov 26, 2011 at 04:52 PM 0
Share

I believe to achieve that i must create a raycast right?

avatar image aldonaletto · Nov 26, 2011 at 05:05 PM 0
Share

Use the same vector to do a Raycast, then check if the object hit is the target; if it's different, something is obscuring the target; if equal, bingo! - the enemy saw the target:

  var targetDir = target.position - transform.position;
  var angle = Vector3.Angle(targetDir, transform.forward);
  if (angle 
NOTE: Target is a transform variable to which you assign the target object in the Inspector (or using GameObject.FindWithTag("Target-tag").transform at Start)
avatar image
0

Answer by RubbaKeys16k · Dec 04, 2011 at 09:57 PM

Tag your player GameObject "Player", tag other objects as something else (set the tags by naming Elements in the tag manager), and there you go - the code below determines what the ray hit first:

 if (Physics.Raycast(transform.position, dir, hit, distance))
 {
     if (hit.collider.gameObject.tag == "Player")
     {
     //Player has been seen
     }    
     if (hit.collider.gameObject.tag != "Player")
     {
     //Player hasn't been seen
     }    
 }
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 RubbaKeys16k · Dec 04, 2011 at 09:57 PM

To work out when the object has reached a position, just put in an invisible object and check for a collision, or that the distance to it is less than a certain amount (as below).

 var distancez = (target.position - enemy.position).magnitude;
     if (distancez < 5)
     {
     //close enough, so trigger whatever needs to happen
     }
 else
 {
 //out of range so keep moving
 }
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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fov problem for enemy 0 Answers

2D enemy Field of Vision script 1 Answer

How do I stop a function from executing? 1 Answer

FOV sensitive Lock-on? 1 Answer

Repeat a function only when an action occurs 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