Raphafrei

ZombieAI

Apr 8th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4.  
  5. [RequireComponent(typeof(NavMeshAgent))]
  6. public class ZombieAI : MonoBehaviour {
  7.  
  8. private NavMeshAgent NMA;
  9. public GameObject player;
  10.  
  11. public Vector3 distanceFromPlayer;
  12.  
  13. public float timeToAttack = 2;
  14. private float currentTimeToAttack;
  15.  
  16. public Animation zombieAnim;
  17. public AnimationClip[] animations;
  18.  
  19. public int damage;
  20.  
  21. public bool canUp;
  22. public bool canMove;
  23. public bool isAttacking;
  24. public bool isDead;
  25.  
  26. void Start () {
  27. damage = Random.Range(5, 10);
  28. NMA = this.GetComponent<NavMeshAgent>();
  29. }
  30.  
  31. void Update () {
  32. distanceFromPlayer = this.transform.position - player.transform.position;
  33.  
  34. canMove = !isAttacking;
  35.  
  36. if(currentTimeToAttack > timeToAttack) {
  37. currentTimeToAttack = 0;
  38. isAttacking = false;
  39. canUp = false;
  40. }
  41.  
  42. // if(distanceFromPlayer.x >= -0.2f && distanceFromPlayer.x <= 0 && distanceFromPlayer.z >= -0.75f && distanceFromPlayer.y <= -0.5f && currentTimeToAttack == 0) {
  43. //
  44. // isAttacking = true;
  45. // Attack();
  46. // }
  47.  
  48. if (canUp) {
  49. currentTimeToAttack += Time.deltaTime;
  50. }
  51.  
  52. // if (!canMove && !isAttacking) {
  53. // zombieAnim.clip = animations[3];
  54. // zombieAnim.Play();
  55. // }
  56.  
  57. if (canMove && !isDead) {
  58. zombieAnim.clip = animations[0];
  59. zombieAnim.Play();
  60. NMA.SetDestination(player.transform.position);
  61. }
  62. }
  63.  
  64. private void Attack() {
  65. player.GetComponent<PlayerStats>().life -= damage;
  66. isAttacking = true;
  67. canUp = true;
  68. }
  69.  
  70. void OnTriggerEnter(Collider other) {
  71. if (other.gameObject.tag == "Player") {
  72. canMove = false;
  73. zombieAnim.clip = animations[1];
  74. zombieAnim.Play();
  75. Attack();
  76. }
  77. }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment