Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

using UnityEngine;

using System.Collections;

public class AISenses : MonoBehaviour {

public GameObject exclamationMark;


public GameObject questionMark;
public AudioClip alarm;

public float hearingRadius;


public float FOVAngle;
public float sightDistance;

public GameObject player;

//AI Image Gameobjects


void Start () {
exclamationMark.SetActive (false);
questionMark.SetActive (false);
}

//If the AI freaks out


void FreakOut()
{
//Setup Alarm phase, the player loses a life
exclamationMark.SetActive (true);
questionMark.SetActive (false);
AudioSource.PlayOneShot (alarm, transform.position);
GameVariables.playerLives --;

}
void Update () {

//sets up the UI alert images


if (CanHear (player)) {
questionMark.SetActive (true);
} else {
questionMark.SetActive (false);
}

if (CanSee (player)) {
FreakOut();
} else {
exclamationMark.SetActive (false);
}

//The AI constantly checking the players position to see if it's within


hearing distance
public bool CanHear ( GameObject target ) {

return (Vector3.Distance (target.transform.position,


transform.position) <=
hearingRadius + target.GetComponent<AIHearable>().soundLevel);

public bool CanSee ( GameObject target) {


//if the player is within line of sit
if (IsInLOS (target) && IsInFOV (target)) {

//Go to Freakout Phase


FreakOut ();
return true;
} else {
return false;
}

//Using a raycast to check if the player is in front of it


public bool IsInLOS ( GameObject target ) {

Vector3 vectorToTarget = (target.transform.position -


transform.position);
RaycastHit hitInfo;

if (Physics.Raycast (transform.position, vectorToTarget, out hitInfo,


sightDistance)) {
// if the player is there

if (hitInfo.collider.gameObject == target)
{
//it turns true
return true;
}
else
{
//if not then the game continues
//Debug.Log (hitInfo.collider.gameObject.name + " is in the
way ");
return false;
}
} else {
// We didn't hit anything with the ray -- so... the target is too
far away
return false;
}

//The AI is using peripherals checking if the player is within the vicinity


public bool IsInFOV ( GameObject target ) {

//constantly checkin if the player is within distance to be seen


Vector3 vectorToTarget = (target.transform.position -
transform.position);
float angleToTarget = Vector3.Angle (transform.forward,
vectorToTarget);

//if the player is in front...


if (angleToTarget <= FOVAngle) {

//then its true and it takes action


return true;
} else {
//if not the game continues
return false;
}

You might also like