Search Unity

Enemy AI Unable to See In Varying Heights?

Discussion in 'Editor & General Support' started by MetroidJunkie2015, Jan 11, 2019.

  1. MetroidJunkie2015

    MetroidJunkie2015

    Joined:
    Nov 8, 2015
    Posts:
    31
    I have an empty attached to the enemy that constantly turns to face you and casts raycasts to determine if you're in the enemy's sight and not behind a wall. It works just fine if the player is around the same height level as the enemy but, if you're below them even a slight amount, they seem to act as though they can't see you at all. Here's the script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SightSpot : MonoBehaviour
    6. {
    7.  
    8.     GameObject Player;
    9.  
    10.     void Start ()
    11.     {
    12.         Player = GameObject.Find("MainCharacter");
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         if (Player != null)
    18.         {
    19.             Vector3 targetPostition = new Vector3(Player.transform.position.x,
    20.                                            this.transform.position.y,
    21.                                            Player.transform.position.z);
    22.             this.transform.LookAt(targetPostition);
    23.         }
    24.     }
    25. }
    26.  
    I know this is due to the y axis being excluded but, for some reason, setting the targetPosition's transform.position.y to Player causes the enemy to become completely blind. Is there a way around this?