I’ve been trying for the past few days trying to figure out a way to visual debug a field of view angle.
I’ve tried it multiple ways with a custom editor and a mesh but its too confusing for me. Is there a simpler way to do this?
I have this field of view script for enemies to see the player. Everything works fine, but I could really use the debug to figure out view ranges.
I’ll post the code below:
void Update () {
inCell = player.GetComponent<PlayerMovement> ().playerInCell;
if(inCell == true)
{
Patrol ();
}
else
{
CheckForLights ();
FieldOfView ();
//Debug.Log ("A luz da cena está: " + sceneLightStatus + " e o player tem a luz: " + playerLightStatus);
if(playerInSight == true && playerInRange == true)
{
inPursuit = true;
agent.SetDestination (target.transform.position);
}
else
{
Patrol ();
}
}
}
void FixedUpdate()
{
Vector3 raycastDirection = target.transform.position - transform.position;
RaycastHit hit;
if(Physics.Raycast (transform.position, raycastDirection, out hit, viewDistance) && hit.transform.tag == "Player")
{
playerInRange = true;
}
else{
Invoke ("ChangePlayerInRange", exitTime);
}
}
void FieldOfView()
{
Vector3 targetDir = target.position - transform.position;
float angle = Vector3.Angle (targetDir, transform.forward);
minimumDistance = Vector3.Distance (target.transform.position, transform.position);
if(angle < FOV * 0.5f && minimumDistance > 2f)
{
playerInSight = true;
}
else if(minimumDistance < 2f){
playerBusted = true;
playerInSight = true;
}
else{
Invoke ("ChangePlayerInSight", exitTime);
//playerInSight = false;
}
}