- Home /
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);
}
}
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
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.
start out by making a small change to your for loop:
for (int i = 0; i < count; i++)
I did do that before and nothing happens now when i play the game @Zaeran
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;
}
Your answer
Follow this Question
Related Questions
Enemy Ai Field of View 0 Answers
Navmeshagent wont go after player after stopping! 2 Answers
models in unity doesn't look as in blender 2 Answers
Help with Ai Field of view Script 0 Answers