• 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 Gabimela · Sep 17, 2020 at 10:25 PM · unity 5enemy aifieldofviewfield of vision

Trying to create a field of view for the enemies

Hi guys, so im trying to create a field of view for the enemies in my game, i was following a tutorial but its not working now and i really don't understand where i've gone wrong.The error is :

IndexOutOfRangeException: Index was outside the bounds of the array. FieldOfViewEnemy.inFOV (UnityEngine.Transform checkingObject, UnityEngine.Transform target, System.Single MaxAngle, System.Single MaxRadius) (at Assets/Scripts/FieldOfViewEnemy.cs:26) FieldOfViewEnemy.Update () (at Assets/Scripts/FieldOfViewEnemy.cs:63)

and the code for it is:

 using System.Collections;
 using System.Collections.Generic;
 using System.Collections.Specialized;
 using System.Security.Cryptography;
 using UnityEngine;



 public class FieldOfViewEnemy : MonoBehaviour
 {

 public Transform player;
 public float MaxAngle;
 public float MaxRadius;

 private bool isInFOV = false;

 public bool inFOV(Transform checkingObject, Transform target, float MaxAngle, float MaxRadius)
 { 

     Collider[] overlaps = new Collider[10];
     int count = Physics.OverlapSphereNonAlloc(checkingObject.position, MaxRadius, overlaps);

     for (int i = 0; i < count + 1; i++)
     {
         if (overlaps[i] != null)
         {


             if (overlaps[i].transform == target)
             {
                 Vector3 directionBetween = (target.position - checkingObject.position).normalized;
                 directionBetween.y *= 0;

                 float angle = Vector3.Angle(checkingObject.forward, directionBetween);

                 if (angle <= MaxAngle)
                 {
                     Ray ray = new Ray(checkingObject.position, target.position - checkingObject.position);
                     RaycastHit hit;

                     if (Physics.Raycast(ray, out hit, MaxRadius))
                     {
                         if (hit.transform == target)
                             Debug.Log("player spotted!");
                             return true;
                    
                         


                     }

                 }
             }
         }
     }
     return false;
 }
  

    void Update()
     {
         isInFOV = inFOV(transform, player, MaxAngle, MaxRadius);
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by yusfkoc15 · Sep 27, 2020 at 11:43 AM

Your array range is small. İnstead of ----Collider[] overlaps = new Collider[10];-----

Maybe using list ıt can help you

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 Zaeran · Sep 18, 2020 at 03:00 AM

Most likely it's your for loop.

You're getting the count, which is the number of elements in the overlaps array, but then your for loop goes up to 'count + 1', which is always going to end up with a number that's one higher than the number of elements in your array.

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 Gabimela · Sep 18, 2020 at 08:51 AM 0
Share

i see, so how would i fix this? @Zaeran

avatar image Zaeran Gabimela · Sep 18, 2020 at 11:04 AM 0
Share

start out by making a small change to your for loop:

 for (int i = 0; i < count; i++)
avatar image Gabimela Zaeran · Sep 18, 2020 at 11:41 AM 0
Share

I did do that before and nothing happens now when i play the game @Zaeran

avatar image
0

Answer by SpatenDeamon · Sep 26, 2020 at 01:25 PM

It's a different Way but Maybe it can help you.

Don't use EnemyInSight() in Update. Instead you can use Invoke Repeating.

 bool EnemyinfieldOfView(Transform looker, float maxAngel, Transform targeter)
     {
         if (targeter != null)
         {
             Vector3 targetDir = targeter.transform.position - transform.position;
             float angel = Vector3.Angle(targetDir, looker.forward);
             // shoot a ray to the direction of target
             Vector3 direction = targeter.transform.position - looker.position;
             RaycastHit hit;
             if (Physics.Raycast(looker.position, direction, out hit))
             {
                 // check if the ray hits the target
                 if (hit.transform == targeter.transform)
                 {
                     if (angel < maxAngel)
                     {
                         return true;
                     }
                 }
                 return false;
             }
             return false;
 
 
         }
         return false;
     }
     public GameObject EnemyInSight()
     { 
         // All gamobjects from enemyteam are checked whether are in field of view
         GameObject[] gos;
         gos = GameObject.FindGameObjectsWithTag(targetTeam);
         GameObject enemyinsight = null;
         foreach (GameObject go in gos)
         {
            float distance = Vector3.Distance(transform.position, go.transform.position);
            if(distance < target_finddistance && EnemyinfieldOfView(transform, target_findangel, go.transform))
             {
                 enemyinsight = go;
                 Debug.Log(enemyinsight);
                 return enemyinsight;
             }
         }
         return enemyinsight;
     }
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

234 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 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 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

Navmeshagent wont go after player after stopping! 2 Answers

models in unity doesn't look as in blender 2 Answers

Causing enemies to slide around walls 1 Answer

Help with Ai Field of view Script 0 Answers

Object bounces off wall, but on return goes through it 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