Advertisement
Guest User

xd

a guest
Aug 31st, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CFDG 5.98 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class EnemyMovement : MonoBehaviour
  5. {
  6.     [Range(0, 360)]
  7.     public float enemyFOV = 120f;
  8.     [Range(5, 30)]
  9.     public float detectionRange = 15f;
  10.  
  11.     private float deadZone = 5f;
  12.     private NavMeshAgent nav;
  13.     private GameObject player;
  14.     private Animator anim;
  15.     private HashIDs hash;
  16.     private SphereCollider sCollider;
  17.     private EnemyAnimatorHelper animatorHelper;
  18.     private Health health;
  19.  
  20.     private bool playerInFOV;
  21.     private bool playerInHearRange;
  22.  
  23.     private CapsuleCollider cCollider;
  24.    
  25.     private Vector3 lastKnownPosition;
  26.     private float speed;
  27.     private float angle;
  28.     private bool enemyAnimIsDead;
  29.  
  30.     private float timer = 0.0f;
  31.     private AudioSource footStepSound;
  32.  
  33.     void Awake()
  34.     {
  35.         nav = GetComponent<NavMeshAgent>();
  36.         anim = GetComponent<Animator>();
  37.         sCollider = GetComponent<SphereCollider>();
  38.         health = GetComponent<Health>();
  39.         cCollider = GetComponent<CapsuleCollider>();
  40.         player = GameObject.FindGameObjectWithTag(Tags.player);
  41.         hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
  42.         animatorHelper = new EnemyAnimatorHelper(anim, hash);
  43.  
  44.         nav.updateRotation = false;
  45.  
  46.         deadZone *= Mathf.Deg2Rad;
  47.  
  48.         anim.SetLayerWeight(1, 1f);
  49.  
  50.         lastKnownPosition = transform.position;
  51.  
  52.         playerInFOV = false;
  53.         playerInHearRange = false;
  54.         speed = 0f;
  55.         angle = 0f;
  56.  
  57.         sCollider.radius = detectionRange;
  58.  
  59.         footStepSound = GetComponent<AudioSource>();
  60.  
  61.         enemyAnimIsDead = true;
  62.     }
  63.  
  64.     void OnAnimatorMove()
  65.     {
  66.  
  67.         nav.velocity = anim.deltaPosition / Time.deltaTime;
  68.         transform.rotation = anim.rootRotation;
  69.  
  70.     }
  71.  
  72.     void Update()
  73.     {
  74.  
  75.         if (health.currentHealth > 0)
  76.         {  
  77.  
  78.         DetectionManager();
  79.         DecisionManager();
  80.         Debug.DrawLine(lastKnownPosition, lastKnownPosition + Vector3.up * 4, Color.blue);
  81.  
  82.         if (playerInFOV)
  83.             Debug.DrawLine(lastKnownPosition, lastKnownPosition + new Vector3(1, 2, 0), Color.red);
  84.         if (playerInHearRange)
  85.             Debug.DrawLine(lastKnownPosition, lastKnownPosition + new Vector3(-11, 2, 0), Color.green);
  86.         }
  87.         else
  88.         {
  89.             DeathAnimation();
  90.  
  91.             timer += Time.deltaTime;
  92.             {
  93.                 if (timer > 5.0f)
  94.  
  95.                  Destroy(this.gameObject);
  96.             }
  97.         }
  98.  
  99.     }
  100.  
  101.     void OnTriggerStay(Collider other)
  102.     {
  103.         if (other.gameObject == player)
  104.         {
  105.            
  106.             lastKnownPosition = player.transform.position;
  107.             playerInHearRange = true;
  108.             Debug.DrawLine(transform.position + Vector3.up, lastKnownPosition + Vector3.up, Color.green);
  109.  
  110.         }
  111.  
  112.     }
  113.  
  114.     void OnTriggerExit(Collider other)
  115.     {
  116.         if (other.gameObject == player)
  117.         {
  118.  
  119.             lastKnownPosition = player.transform.position;
  120.             playerInFOV = false;
  121.             playerInHearRange = false;
  122.         }
  123.     }
  124.     void DetectionManager()
  125.     {
  126.         DetectionManager(lastKnownPosition);
  127.     }
  128.  
  129.     void DetectionManager(Vector3 position)
  130.     {
  131.         if (Vector3.Distance(transform.position, position) < sCollider.radius)
  132.         {
  133.             Vector3 direction = position - transform.position;
  134.             float signAngle = Vector3.Angle(direction, transform.forward);
  135.  
  136.             if (signAngle < enemyFOV * 0.5f)
  137.             {
  138.                 RaycastHit hit;
  139.  
  140.                 if (Physics.Raycast(transform.position + transform.up, direction.normalized, out hit, sCollider.radius))
  141.                 {
  142.                     if (hit.collider.gameObject == player)
  143.                     {
  144.  
  145.  
  146.                         Debug.DrawLine(transform.position + Vector3.up, position + Vector3.up, Color.red);
  147.                         playerInFOV = true;
  148.                     }
  149.                     else
  150.                     {
  151.                         playerInFOV = false;
  152.                     }
  153.                 }
  154.  
  155.  
  156.             }
  157.  
  158.         }
  159.  
  160.  
  161.  
  162.     }
  163.  
  164.  
  165.     void DecisionManager()
  166.     {
  167.         angle = FindAngle(transform.forward, lastKnownPosition - transform.position, transform.up);
  168.  
  169.         if(Vector3.Distance(lastKnownPosition, transform.position) < 1.5f)
  170.         {
  171.             if (playerInHearRange)
  172.             {
  173.                 MovementManager(0, angle);
  174.             }
  175.             MovementManager(0, 0);
  176.             return;
  177.         }
  178.         else
  179.         {
  180.  
  181.             if(playerInHearRange)
  182.             {
  183.                 //turn in sound direction
  184.                 speed = 1f;
  185.  
  186.             }
  187.  
  188.             if (playerInFOV)
  189.             {
  190.                 //attack
  191.                 speed = 4f;
  192.             }
  193.  
  194.             MovementManager(speed, angle);
  195.             return;
  196.         }
  197.  
  198.     }
  199.  
  200.  
  201.     void MovementManager(float speed = 0, float angle = 0)
  202.     {
  203.         if(Mathf.Abs(angle)< deadZone)
  204.         {
  205.             transform.LookAt(transform.position + nav.desiredVelocity);
  206.             angle = 0;
  207.  
  208.         }
  209.  
  210.  
  211.  
  212.         animatorHelper.Setup(speed, angle);
  213.     }
  214.  
  215.     private void DeathAnimation()
  216.     {
  217.         if (enemyAnimIsDead)
  218.  
  219.         {
  220.             anim.SetTrigger(hash.isDead);
  221.             cCollider.isTrigger = true;
  222.             enemyAnimIsDead = false;
  223.         }
  224.     }
  225.  
  226.     float FindAngle(Vector3 fromVector, Vector3 toVector, Vector3 upVector)
  227.     {
  228.         if (toVector == Vector3.zero)
  229.             return 0f;
  230.  
  231.         float angle = Vector3.Angle(fromVector, toVector);
  232.         Vector3 normal = Vector3.Cross(fromVector, toVector);
  233.         angle *= Mathf.Sign(Vector3.Dot(normal, upVector));
  234.  
  235.         angle *= Mathf.Deg2Rad;
  236.  
  237.         return angle;
  238.     }
  239.  
  240.     void FootSteps()
  241.     {
  242.  
  243.         footStepSound.pitch = UnityEngine.Random.Range(0.9f, 1.1f);
  244.         footStepSound.PlayOneShot(footStepSound.clip);
  245.     }
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement