Enemy AI, detect player when seen

Hey guys,

O.k so I am working on a project for college but I am a total Noob when it comes to Unity and scripting, to be honest I have no idea why we are making a game so early but there you go! Anyway, I have just about everything working but I need help with enemy AI. It’s a stealth game so I am really looking for a simple script that allows the enemy to patrol and attack is he spots the player. I found this script and it sounds exactly like what I need but I cannot get it to work. Also, the rest of the game is in c# and i think this is a unityscript. It says to attach it to place it in the update function but when I try to run it I get "Unknown identifier : ‘maxDistance’. and "Unknown identifier : ‘playerObject’. errors. I’m sure it’s an easy fix but I just don’t know how to, any help would be HUGELY appreciated because the project is due tomorrow!

Here’s the script

//if an enemy as further than maxDistance from you, it cannot see you
var maxDistanceSquared = maxDistance * maxDistance;
var rayDirection : Vector3 = playerObject.transform.localPosition - transform.localPosition;
var enemyDirection : Vector3 = transform.TransformDirection(Vector3.forward);
var angleDot = Vector3.Dot(rayDirection, enemyDirection);
var playerInFrontOfEnemy = angleDot > 0.0;
var playerCloseToEnemy = rayDirection.sqrMagnitude < maxDistanceSquared;
 
if ( playerInFrontOfEnemy && playerCloseToEnemy)
{
//by using a Raycast you make sure an enemy does not see you
//if there is a bulduing separating you from his view, for example
//the enemy only sees you if it has you in open view
var hit : RaycastHit;
if (Physics.Raycast (transform.position,rayDirection, hit, maxDistance)
&& hit.collider.gameObject==playerObject) //player object here will be your Player GameObject
{
//enemy sees you - perform some action
}
else
{
//enemy doesn't see you
}	
}

Once you got that, now work on the AI notice the player when within a certain distance. SO, take the different of the AI and the player, if it is less than AI_Notice_Distance then have the AI acknowledge the player’s existence. SO, you can have it rotate to the player or something. GameObject.Find(…), transform.position.distance(player.position,AI.position)

From there you can then make it slight more complex by obscuring the AI’s view, so if there is a wall between the player and the AI, then the AI will not acknowledge the player. TO do this you can cast a Ray from the AI to the player and see if anything is obscuring. If you want more accuracy then you will use more Raycast along the vertical axis, in case of short wall or some other objects. Check out Raycast and HitCast in the manual.

From there you can then do soo many more things to improve AI. AI is amongst the hardest task to mimic that of human interaction and is very in depth field. Check out some free reads online about AI and what more could you do.

Here’s a low tech but effective solution which incorporates Raycast with Commandos style ‘scanning’ - not visible to player but covers a cone of vision of the enemy. This code also draws a line in debug to show how the Raycast is behaving. If player is hidden behind a collider, enemy Raycast is blocked and enemy doesn’t see the player - Player GameObject needs to have a tag assigned “Player”.

Don’t forget to declare LeftRightZ bool. ‘MEyes’ is a child object of the enemy where their eyes are. Feel free to play with the numbers or replace with variables but this one works for my game.

if(LeftRightZ)
            {
                if(EyeScanZ < 30)
                {
                    EyeScanZ += 100 * Time.deltaTime;
                }
                else
                {
                    LeftRightZ = false;
                }
            }
            else
            {
                if (EyeScanZ > -30)
                {
                    EyeScanZ -= 100 * Time.deltaTime;
                }
                else
                {
                    LeftRightZ = true;
                }
            }
            transform.Find("MEyes").transform.localEulerAngles = new Vector3(0, EyeScanZ);
    
    
            RaycastHit hit;
            Debug.DrawRay(transform.Find("MEyes").position, transform.Find("MEyes").transform.forward * ViewDistance);
    
            if (Physics.Raycast(transform.Find("MEyes").position, transform.Find("MEyes").transform.forward * ViewDistance, out hit, ViewDistance))
            {
                if(hit.transform.gameObject.tag == "Player")
                {
                    Debug.Log(gameObject.name + " CAN see Player");
                }
    
            }

,Here’s a low tech but effective solution which incorporates Raycast with Commandos style ‘scanning’ - not visible to player but covers a cone of vision of the enemy. This code also draws a line in debug to show how the Raycast is behaving. If player is hidden behind a collider, enemy Raycast is blocked and enemy doesn’t see the player - Player GameObject needs to have a tag assigned “Player”.

Don’t forget to declare LeftRightZ bool. ‘MEyes’ is a child object of the enemy where their eyes are. Feel free to play with the numbers or replace with variables but this one works for my game.

        if(LeftRightZ)
        {
            if(EyeScanZ < 30)
            {
                EyeScanZ += 100 * Time.deltaTime;
            }
            else
            {
                LeftRightZ = false;
            }
        }
        else
        {
            if (EyeScanZ > -30)
            {
                EyeScanZ -= 100 * Time.deltaTime;
            }
            else
            {
                LeftRightZ = true;
            }
        }
        transform.Find("MEyes").transform.localEulerAngles = new Vector3(0, EyeScanZ);


        RaycastHit hit;
        Debug.DrawRay(transform.Find("MEyes").position, transform.Find("MEyes").transform.forward * ViewDistance);

        if (Physics.Raycast(transform.Find("MEyes").position, transform.Find("MEyes").transform.forward * ViewDistance, out hit, ViewDistance))
        {
            if(hit.transform.gameObject.tag == "Player")
            {
                Debug.Log(gameObject.name + " CAN see Player");
            }

        }