Search Unity

Field Of View Script not working properly

Discussion in 'Scripting' started by denissuu, Feb 17, 2020.

  1. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162
    Hi there!

    I have little issue with my FOV script, everything is displaying fine in the editor, but it seems that my Player isn't getting detected properly when he is in the FOV radius.
    It seems that while my characater is in his POV, the line isn't really being constantly green, it gets red like a quarter of the time.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FOVDetection : MonoBehaviour
    6. {
    7.     public Transform player;
    8.     public float maxAngle;
    9.     public float maxRadius;
    10.     public float heightMultiplayer = 1.5f;
    11.     private bool isInFov = false;
    12.  
    13.     public void OnDrawGizmos()
    14.     {
    15.         Gizmos.color = Color.yellow;
    16.         Gizmos.DrawWireSphere(transform.position, maxRadius);
    17.  
    18.         Vector3 fovLine1 = Quaternion.AngleAxis(maxAngle, transform.up) * transform.forward * maxRadius;
    19.         Vector3 fovLine2 = Quaternion.AngleAxis(-maxAngle, transform.up) * transform.forward * maxRadius;
    20.  
    21.         Gizmos.color = Color.blue;
    22.         Gizmos.DrawRay(transform.position, fovLine1);
    23.         Gizmos.DrawRay(transform.position, fovLine2);
    24.  
    25.         if (isInFov == false)
    26.         {
    27.             Gizmos.color = Color.red;
    28.         }
    29.         if (isInFov == true)
    30.         {
    31.             Gizmos.color = Color.green;
    32.         }
    33.         Gizmos.DrawRay(transform.position, (player.transform.position - transform.position).normalized * maxRadius);
    34.  
    35.         Gizmos.color = Color.black;
    36.         Gizmos.DrawRay(transform.position, transform.forward * maxRadius);
    37.     }
    38.  
    39.     public void inFOV(Transform checkingObject, Transform target, float maxAngle, float maxRadius)
    40.     {
    41.         Vector3 directionBetween = (target.position - checkingObject.position).normalized;
    42.         directionBetween.y *= 0;
    43.  
    44.         RaycastHit hit;
    45.         if (Physics.Raycast(checkingObject.position + Vector3.zero, (target.position - checkingObject.position).normalized, out hit, maxRadius))
    46.         {
    47.             if (LayerMask.LayerToName(hit.transform.gameObject.layer) == "Player")
    48.             {
    49.                 float angle = Vector3.Angle(checkingObject.forward + Vector3.zero, directionBetween);
    50.  
    51.                 if (angle <= maxAngle)
    52.                 {
    53.                     isInFov = true;
    54.                 }
    55.                 else
    56.                 {
    57.                     isInFov = false;
    58.                 }
    59.             }
    60.             else
    61.             {
    62.                 isInFov = false;
    63.             }
    64.         }
    65.  
    66.     }
    67.     private void FixedUpdate()
    68.     {
    69.         inFOV(transform, player, maxAngle, maxRadius);
    70.  
    71.         if (isInFov)
    72.         {
    73.             Debug.Log("hau");
    74.         }
    75.     }
    76. }
    77.  

    If anyone could explain why this is not working/provide a fix I would really appreciate it!
     
    Last edited: Feb 17, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    It could be a ton of things given the code above. Not finding the player. Not calculating from the right data. Script turned off, etc.

    You are gonna have to start making some onscreen output to tell the intermediate values for angle, if it is even hitting the player, the resultant isInFOV, etc., etc. Just put a UI.Text item up on the screen and start dumping "interesting" information from different parts of the routines above out so you can track down what is happening.
     
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    well that scripts relies heavily on a well-formed collider mesh. maybe your collider is bad.