Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.35 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5.  
  6. public class EnemyMovement : MonoBehaviour
  7. {
  8.     public float fov = 120f;
  9.     public float loseThreshold = 10f;
  10.     public float viewDistance = 10f;
  11.     public float wonderRadius = 10f;
  12.     public float rotationMultiplyer = 15f;
  13.  
  14.     private bool isAware = false;
  15.     private bool isDetecting = false;
  16.     private float loseTimer = 0;
  17.     private float playerDistance;
  18.  
  19.     private Vector3 wanderPoint;
  20.     Transform player;
  21.     NavMeshAgent nav;
  22.     public Transform shoot;
  23.  
  24.  
  25.  
  26.     private void Awake()
  27.     {
  28.         player = GameObject.FindGameObjectWithTag("Player").transform;
  29.  
  30.         nav = GetComponent<NavMeshAgent>();
  31.         wanderPoint = RandomNavPoint();
  32.     }
  33.  
  34.     private void Update()
  35.     {
  36.  
  37.         Chase();
  38.     }
  39.  
  40.     public void SearchForPlayer()
  41.     {
  42.         if (Vector3.Angle(Vector3.forward, transform.InverseTransformPoint(player.position)) < fov / 2f)
  43.         {
  44.             if (Vector3.Distance(player.position, transform.position) < viewDistance)
  45.             {
  46.                 RaycastHit hit;
  47.                 if (Physics.Linecast(transform.position, player.position, out hit, -1))
  48.                 {
  49.                     if (hit.transform.CompareTag("Player"))
  50.                     {
  51.                         OnAware();
  52.                     }
  53.                     else
  54.                     {
  55.                         isDetecting = false;
  56.                     }
  57.                 }
  58.                 else
  59.                 {
  60.                     isDetecting = false;
  61.                 }
  62.             }
  63.             else
  64.             {
  65.                 isDetecting = false;
  66.             }
  67.         }
  68.         else
  69.         {
  70.             isDetecting = false;
  71.         }
  72.     }
  73.  
  74.     public void OnAware()
  75.     {
  76.         isAware = true;
  77.         isDetecting = true;
  78.         loseTimer = 0f;
  79.     }
  80.  
  81.     public void Chase()
  82.     {
  83.         if (isAware)
  84.         {
  85.             FacePlayer();
  86.             nav.SetDestination(player.position);
  87.             if (!isDetecting)
  88.             {
  89.                 loseTimer += Time.deltaTime;
  90.                 if (loseTimer >= loseThreshold)
  91.                 {
  92.                     isAware = false;
  93.                     loseTimer = 0f;
  94.                 }
  95.             }
  96.         }
  97.         else
  98.         {
  99.             Wander();
  100.  
  101.         }
  102.         SearchForPlayer();
  103.     }
  104.  
  105.    void FacePlayer()
  106.     {
  107.         Vector3 direction = (player.position - transform.position).normalized;
  108.         Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
  109.         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * rotationMultiplyer);
  110.     }
  111.  
  112.  
  113.     public void Wander()
  114.     {
  115.         if (Vector3.Distance(transform.position, wanderPoint) < 5f)
  116.         {
  117.             wanderPoint = RandomNavPoint();
  118.         }
  119.         else
  120.         {
  121.             nav.SetDestination(wanderPoint);
  122.         }
  123.     }
  124.  
  125.     public Vector3 RandomNavPoint()
  126.     {
  127.         Vector3 randomPoint = (Random.insideUnitSphere * wonderRadius) + transform.position;
  128.         NavMeshHit navHit;
  129.         NavMesh.SamplePosition(randomPoint, out navHit, wonderRadius, -1);
  130.         return new Vector3(navHit.position.x, transform.position.y, navHit.position.z);
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement