#So I could use some input on a enemy
1 messages · Page 1 of 1 (latest)
I'm having some issues with the angles the enemy is spotting the player from
see the pictures below:
the green line indicates that the enemy can see the player
I want the player to be spotted when inside the yellow lines on picture 2
it shouldn't be spotted on picture 1
also, the FOV is pointing upwards when I want it to point forwards
I didn't write the code myself, just made some changes to convert it to "2D"
here are the two scripts:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FieldOfView : MonoBehaviour
{
public float radius;
[Range(0,360)]
public float angle;
public GameObject playerRef;
public LayerMask targetMask;
public LayerMask obstructionMask;
public bool canSeePlayer;
private void Start()
{
playerRef = GameObject.FindGameObjectWithTag("Player");
StartCoroutine(FOVRoutine());
}
private IEnumerator FOVRoutine()
{
WaitForSeconds wait = new WaitForSeconds(0.2f);
while (true)
{
yield return wait;
FieldOfViewCheck();
}
}
private void FieldOfViewCheck()
{
Collider[] rangeChecks = Physics.OverlapSphere(transform.position, radius, targetMask);
if (rangeChecks.Length != 0)
{
Transform target = rangeChecks[0].transform;
Vector3 directionToTarget = (target.position - transform.position).normalized;
if (Vector3.Angle(transform.forward, directionToTarget) < angle / 2)
{
float distanceToTarget = Vector3.Distance(transform.position, target.position);
if (!Physics.Raycast(transform.position, directionToTarget, distanceToTarget, obstructionMask))
canSeePlayer = true;
else
canSeePlayer = false;
}
else
canSeePlayer = false;
}
else if (canSeePlayer)
canSeePlayer = false;
}
}```
and the editor script:
using UnityEngine;
[CustomEditor(typeof(FieldOfView))]
public class FieldOfViewEditor : Editor
{
private void OnSceneGUI()
{
FieldOfView fov = (FieldOfView)target;
Handles.color = Color.white;
Handles.DrawWireArc(fov.transform.position, Vector3.forward, Vector3.right, 360, fov.radius);
Vector3 viewAngle01 = DirectionFromAngle(fov.transform.eulerAngles.z, -fov.angle / 2);
Vector3 viewAngle02 = DirectionFromAngle(fov.transform.eulerAngles.z, fov.angle / 2);
Handles.color = Color.yellow;
Handles.DrawLine(fov.transform.position, fov.transform.position + viewAngle01 * fov.radius);
Handles.DrawLine(fov.transform.position, fov.transform.position + viewAngle02 * fov.radius);
if (fov.canSeePlayer)
{
Handles.color = Color.green;
Handles.DrawLine(fov.transform.position, fov.playerRef.transform.position);
}
}
private Vector3 DirectionFromAngle(float eulerY, float angleInDegrees)
{
angleInDegrees += eulerY;
return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), Mathf.Cos(angleInDegrees * Mathf.Deg2Rad), 0);
}
}```
But u want to work in 3d or in 2d?
3D, but seen from the side
the easiest way was to convert the field of view to 3d
the fov used to be in 3D, but since its a sidescroller I didn't really need it to check for things in the Z direction
so how it works now is how I want it to work, I just cant get the angles right
hmmm okok
honestly I can't even tell where "forward" is for the red object (if that is an enemy)
whats the best way to invert the angle?
In this miniseries (2 episodes) we create a system to detect which targets are in our unit's field of view. This is useful for stealth games and the like.
Source code + starting file:
https://github.com/SebLague/Field-of-View
Support the creation of more tutorials:
https://www.patreon.com/SebastianLague
https://www.paypal.me/SebastianLague
check this video
it's the same that I used some moths ago
i'm so close to making the script I have work, so would be great to sort it out 🙂
thanks for your link though!
the videois what u want
If I understand well
when you rotate it along the vertical axis do the yellow fov indicators rotate smoothly around the circle in the expected direction?
and when it starts by facing right, you would expect the yellow indicators to point right as well? (so long as angle < 90)
wait, don't you want to base it on Y rotation tho?
... ah
I think you found an issue 😉
I set it up in the wrong rotation from the start
but the ground is 0 0 0 rotation
im confused
sry im super new to this
ok if I set the rotation to 0 90 90 I get exactly what I want but inverted
the FOV detection was actually flawless (though I forgot to mask & tag player properly and was wondering wtf was going on for a while), it was just the editor script that needed changing
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(FieldOfView))]
public class FieldOfViewEditor : Editor
{
private void OnSceneGUI()
{
FieldOfView fov = (FieldOfView)target;
Handles.color = Color.white;
Handles.DrawWireArc(fov.transform.position, Vector3.up, Vector3.right, 360, fov.radius);
Vector3 viewAngle01 = DirectionFromAngle(fov.transform.eulerAngles.y, -fov.angle / 2);
Vector3 viewAngle02 = DirectionFromAngle(fov.transform.eulerAngles.y, fov.angle / 2);
Handles.color = Color.yellow;
Handles.DrawLine(fov.transform.position, fov.transform.position + viewAngle01 * fov.radius);
Handles.DrawLine(fov.transform.position, fov.transform.position + viewAngle02 * fov.radius);
if (fov.canSeePlayer)
{
Handles.color = Color.green;
Handles.DrawLine(fov.transform.position, fov.playerRef.transform.position);
}
}
private Vector3 DirectionFromAngle(float eulerY, float angleInDegrees)
{
angleInDegrees += eulerY;
return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), 0, Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
}
}
ok so you changed it back to 3D
I dont really need a depth radius since its a sidescroller
Its actually working as i want when setting the rotation to 0 90 90 and the player is looking to the right
ok I got it working, needed to change it to DirectionFromAngle(fov.transform.eulerAngles.y