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 zemog266 · Mar 11, 2013 at 05:12 AM · aienemyfovfield of vision

2D enemy Field of Vision script

Hi, I want to make a script that gives a enemy a field of vision, if the player steps into this field of vision, an alarm is triggered. Its for a 2D platformer, Here's an image to illustrate what I mean. The field of vision will also need to move up and down as it is a camera type enemy.

Any ideas on how I would make a script that does this?

alt text.

enemy fov.jpg (19.7 kB)
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
0

Answer by robertbu · Mar 11, 2013 at 05:46 AM

I have no idea what you mean by a "camera type enemy" and why that means it has to move up and down. But if you can define a vector for where the "eyes are," finding out if an object is in the field of vision is simple. For example, say you were using transform.right for your eye position/direction, you could do something like:

 if (Vector3.Angle(transform.right, playerPosition - EnemyPosition) < 22.5) {
     Debug.Log("I'm seeing the player");
 }

Note Vector3.Angle returns an unsigned value. Also this is the angle from the vector and the code measures to each side, so the actual field of vision would be 45 degress.

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 zemog266 · Mar 11, 2013 at 06:04 AM 0
Share

I meant as in the enemy is a camera (as in the model) and that I would like to move the field of vision up and down so the player can sneak past when it is looking away from the player. Thanks anyway though!

avatar image robertbu · Mar 11, 2013 at 06:08 AM 0
Share

This solution works for the camera. Just use the transform.forward of the camera. Something like:

 if (Vector3.Angle(Camera.main.transform.forwardt, transPlayer.position - Camera.main.transform.position) < 22.5) {
     Debug.Log("I'm seeing the player");
 }


avatar image zemog266 · Mar 11, 2013 at 06:17 AM 0
Share

I didn't know you could use an actual camera, I will try both and see which one I prefer, would be neat to actually show what the enemy is looking at on the screen (I was thinking of trying to do something similar anyway), thanks again man!

avatar image robertbu · Mar 11, 2013 at 06:30 AM 0
Share

Ahhh... you were talking about a "figurative" camera. I don't think you want an actual camera. Go back to the first solution (they are both the same solution, just using different vectors). Note if you game is organized as the picture, the vector you need to use for your angle statement is "-transform.right". There is no transform.left, so just negate the right vector.

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

10 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

Related Questions

AI script troubles 1 Answer

How do I animate my enemy randomly with time? 1 Answer

How can an enemy deplete players health? 0 Answers

AI Field of vision 1 Answer

enemy ai going to player 2 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