Search Unity

Question LookAt to the closest enemy in player facing direction

Discussion in 'Scripting' started by Only4gamers, Jun 27, 2021.

  1. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Hello everyone,
    I am trying to target the gun to the closest enemy in facing direction. Getting closest enemy is easy but problem is enemies will be in different heights.
    3.PNG

    As you can view in above image. If enemy is below stair player gun will lookat arount 30 degree angle but if enemy is in same height as player then gun will lookat around 5-10 degree angle to the enemy. I hope you will understand the situation. If there is any doubt then please ask me. Thanks.
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Not sure what the desired behavior is here. Do you have any code that you already wrote? Or maybe some example of what you are trying to accomplish? Is it wanted to target the enemy under the stairs.. or should only enemies in the actual field of vision be targeted?
     
    Only4gamers likes this.
  3. Only4gamers

    Only4gamers

    Joined:
    Nov 8, 2019
    Posts:
    327
    Yes, I am trying to target enemies only in field of vision. For this I used a mesh collider trigger of cone shape to get all enemies in player facing direction, then try to use scripts to get the closest enemy.
    4.PNG

    Script:
    Code (CSharp):
    1. public List<Collider> enemiesInView;
    2.  
    3.    // Add enemies to the list if they eneter trigger
    4.     void OnTriggerEnter(Collider other)
    5.     {
    6.         if(other.tag == "Enemy" && !enemiesInView.Contains(other))
    7.         {
    8.             enemiesInView.Add(other);
    9.         }
    10.     }
    11. // remove enemies from the list if they exit trigger
    12.     void OnTriggerExit(Collider other)
    13.     {
    14.         if(other.tag == "Enemy" && enemiesInView.Contains(other))
    15.         {
    16.             enemiesInView.Remove(other);
    17.         }
    18.     }
    19.  
    20. void FixedUpdate()
    21.     {
    22.         if(enemiesInView == null)
    23.         {
    24.             Debug.Log("Null");
    25.         }
    26.         else
    27.         {
    28.             transform.LookAt(FindClosestEnemy().transform);
    29.             Debug.Log(FindClosestEnemy().name);
    30.         }
    31.     }
    32.  
    33. public Collider FindClosestEnemy()
    34.     {
    35.         Collider closest = null;
    36.    
    37.         float distance = Mathf.Infinity;
    38.         Vector3 position = transform.position;
    39.  
    40.             foreach (Collider go in enemiesInView)
    41.             {
    42.                 Vector3 diff = go.transform.position - position;
    43.                 float curDistance = diff.sqrMagnitude;
    44.                 if (curDistance < distance)
    45.                 {
    46.                     closest = go;
    47.                     distance = curDistance;
    48.                 }
    49.             }
    50.         return closest;
    51.     }
    But it's not working as expected and throwing following error in Line 6 and 14:
    NullReferenceException: Object reference not set to an instance of an object

    But maybe there is better way to fix this problem.
     
    Last edited: Jun 28, 2021