• 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 Kek_Kek · Jul 09, 2018 at 06:36 PM · fovfieldofview

Help with FOV script.

I need help with what appears to be a simple problem which I can't seem to fix.

If I have the enemy looking at you, he follows you, however, if you're out of his view, he stops.

Heres the script:

void FindVisibleTargets()

{

visibleTargets.Clear();

Collider[] targetsInViewRadius = Physics.OverlapSphere(transform.position, viewRadius, targetMask);

     for (int i = 0; i < targetsInViewRadius.Length; i++)
     {
         Transform target = targetsInViewRadius[i].transform;
         Vector3 dirToTarget = (target.position - transform.position).normalized;
         if (Vector3.Angle(transform.forward, dirToTarget) < viewAngle / 2)
         {
             float dstToTarget = Vector3.Distance(transform.position, target.position);
             if (!Physics.Raycast(transform.position, dirToTarget, dstToTarget, obstacleMask))
             {
                 visibleTargets.Add(target);
                 Debug.Log("Follow");

             }
             else
             {
                 Debug.Log("Stop");
             }
            
         }
     }
 }

It's not saying "Stop" in the console once the character is out of its view. Can anyone help me with this?

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
Best Answer

Answer by DawdleDev · Jul 09, 2018 at 06:56 PM

The code you wrote basically says, "if the other object is further than the distance to the other object, print stop." As you can imagine, this isn't going to work. You need to make it use, "if the other object is further than the distance I can see, print stop." Here's a revised code:

 // This goes somewhere up at the beginning of your code
 public const float VisibleReach = *Some Number*

.

 for (int i = 0; i < targetsInViewRadius.Length; i++)
  {
      Transform target = targetsInViewRadius[i].transform;
      Vector3 dirToTarget = (target.position - transform.position).normalized;
      if (Vector3.Angle(transform.forward, dirToTarget) < viewAngle / 2)
      {
          if (!Physics.Raycast(transform.position, dirToTarget, VisibleReach, obstacleMask))
          {
              visibleTargets.Add(target);
              Debug.Log("Follow");

          }
          else
          {
              Debug.Log("Stop");
          }
      }
  }
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 Kek_Kek · Jul 09, 2018 at 07:19 PM 0
Share

The script is working now, thank you. I would just add that if the character moved out of the radius such as behind the enemy right after its seen, he doesn't follow neither does he stops. I am going to try and find a way to fix that for now.

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

85 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

Related Questions

Visual debugging a Field of view angle 1 Answer

Camera movement in a 2D? (like angry birds) 1 Answer

Is there a way to set the horizontal field of view of a camera? 3 Answers

Clamping values for camera bounds 1 Answer

Help with Ai Field of view Script 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