Currently I am Working On Game Based on A movie, In which player is stuck inside a maze and he has to find keys to unlock doors and solve sum puzzles and gt out of maze also he is followed my zombies. Every thing is ready accept Enemy. I want to Create An Enemy That moves Randomly in side the Maze , if Enemy Sees Player then it moves towards player and attack him or if Enemy Hears a gun Shot then it should Trace the aprrox. position of gun shot.
This is My Enemy Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
using UnityEngine.AI;
public class ScanForPlayer : MonoBehaviour
{
public float viewDistance;
public float viewAngle;
public LayerMask viewMask;
Transform player;
bool playerIsVisible;
public NavMeshAgent agent;
public ThirdPersonCharacter character;
void Start()
{
player = GameObject.FindWithTag("Player").transform;
agent.updateRotation = false;
}
void Update()
{
playerIsVisible = CanSeePlayer ();
if (agent.remainingDistance <= 1f || playerIsVisible || (Vector3.Distance (transform.position, FindObjectOfType<Shooting> ().FirePos) < viewDistance / 2 && FindObjectOfType<Shooting> ().isShooting)) {
AgentMover ();
}
character.Move (agent.desiredVelocity, false, false);
}
bool CanSeePlayer()
{
if (Vector3.Distance(transform.position, player.position) < viewDistance)
{
Vector3 dirToPlayer = (player.position - transform.position).normalized;
float angleBetweenGuardAndPlayer = Vector3.Angle(transform.forward, dirToPlayer);
if (angleBetweenGuardAndPlayer < viewAngle / 2f)
{
if (!Physics.Linecast(transform.position, player.position, viewMask))
{
return true;
}
}
}
return false;
}
Vector3 RandomNavSphere (Vector3 origin, float dist, int layermask)
{
Vector3 randDirection = Random.insideUnitSphere * dist;
randDirection += origin;
UnityEngine.AI.NavMeshHit navHit;
UnityEngine.AI.NavMesh.SamplePosition (randDirection, out navHit, dist, layermask);
if (Vector3.Distance (transform.position, FindObjectOfType<Shooting>().FirePos) < viewDistance/2 && FindObjectOfType<Shooting>().isShooting) {
return FindObjectOfType<Shooting> ().FirePos;
}
return navHit.position;
}
void AgentMover ()
{
if (agent.remainingDistance <= 1f || playerIsVisible || (Vector3.Distance (transform.position, FindObjectOfType<Shooting> ().FirePos) < viewDistance / 2 && FindObjectOfType<Shooting> ().isShooting) || agent.isStoppeds) {
if (Vector3.Distance (transform.position, player.position) > 1) {
if (playerIsVisible) {
agent.SetDestination (player.position);
} else {
agent.SetDestination (RandomNavSphere (transform.position, viewDistance * 10, viewMask));
}
} else {
agent.isStopped = true;
Debug.Log ("Dead");
}
}
}
}