Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Our response to Improbable’s blog post (and why you can keep working on your SpatialOS game). Read more here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Enemy AI Unable to See In Varying Heights?

Discussion in 'Editor & General Support' started by MetroidJunkie2015, Jan 11, 2019 at 2:48 PM.

  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?