- Home /
How to create a field of view for enemy ai that detects a player Unity 2d?
Hello Everyone, as the title suggests, I'm having trouble coding a script in C# that will give my enemy ai a "field of view". This view will be in a shaped as a circle but have a cone that follows the direction of the enemy as it moves. So far, I've followed this great tutorial on YouTube but it seems to only work for 3D and I need this for a 2D top down game.
So far I've managed to tweak the code a bit so the Gizmos that are drawing the raycasts can draw them at the correct angles I was looking for.
public class LineOfSight_Rotation : MonoBehaviour
{
public Transform player;
public float maxAngle;
public float maxRadius;
private bool isInFov = false;
private void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, maxRadius);
Vector3 fovLine1 = Quaternion.AngleAxis(maxAngle, transform.forward) * transform.up * maxRadius;
Vector3 fovLine2 = Quaternion.AngleAxis(-maxAngle, transform.forward) * transform.up * maxRadius;
Gizmos.color = Color.blue;
Gizmos.DrawRay(transform.position, fovLine1);
Gizmos.DrawRay(transform.position, fovLine2);
if (!isInFov)
Gizmos.color = Color.red;
else
Gizmos.color = Color.green;
Gizmos.DrawRay(transform.position, (player.position - transform.position).normalized * maxRadius);
Gizmos.color = Color.black;
Gizmos.DrawRay(transform.position, transform.up * maxRadius);
}
public static 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)
return true;
}
}
}
}
}
return false;
}
private void Update()
{
isInFov = inFOV(transform, player, maxAngle, maxRadius);
}
}
However, when I try to follow the last step of the video which explains how to do the actual detection of the player it doesn't work and the player line (red), doesn't turn green within range. Sooo... this is my main problem thus far and all help would be appreciated!
Answer by tormentoarmagedoom · Jun 26, 2018 at 07:29 AM
Good day.
I did not read the code or watched the tutorial, but if i have the same problem, i will do this:
Create the "animation" field of view for onw side, and the real field of view by other side.
For the real detector, Create a circle collidider mark as trigger centred in the enemy. Then detect if the player is inside the circle. Now you only need to calculate the angle and the distance, to see if is inside the area of the "vision field".
So (for example) if is between 30º and -30º and no more than 500 distance, is inside the field of view.
Bye!!
The samples provided has a method to detect the player already, if you really want to help please take the time to at least skim through the code provided so you know exactly what my question is about.
Answer by davidnibi · May 07, 2020 at 10:19 PM
The code no longer works, or never worked in the first place.
int count = Physics.OverlapSphereNonAlloc(checkingObject.position, maxRadius, overlaps);
doesn't work, and it needed someone in the comments to spend time to figure out why it wasn't working.
Answer by utzsv1989 · Sep 16, 2020 at 07:57 PM
hello , it might be late to give this answer but here it is : you can simply add a higher number of colliders for exemple a numberlike the total of all your gameobjects in the scene that's should ork perfectly; Collider[] overlaps = new Collider[20];,hello , i might be late to give this answer but here it is : you can simply add a higher number of colliders for exemple a numberlike the total of all your gameobjects in the scene that's should ork perfectly; Collider[] overlaps = new Collider[20];
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can I implement Camera.ScreenToWorldPoint(); in the crosshair script? 0 Answers
Error when loading Unity WebGL 1 Answer