zombieBehaviour.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using HQFPSWeapons;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.AI;
  6. using UnityEngine.Events;
  7. using Mirror;
  8. public class zombieBehaviour : NetworkBehaviour
  9. {
  10. public Transform player;
  11. public float curDistance;
  12. public float minDistance = 10;
  13. float defaultSpeed;
  14. NavMeshAgent agent;
  15. Animator anim;
  16. [Header("Health")]
  17. [SyncVar]
  18. public float health = 100;
  19. // public GameObject bloodPrefab;
  20. public GameObject BloodAttach;
  21. public GameObject[] BloodFX;
  22. [Header("Attack")]
  23. public float damage = 1;
  24. public float attackTime = 2;
  25. public float t;
  26. public bool acceptCommonItems = true;
  27. public List<GameObject> dropOnDie;
  28. public UnityEvent OnDie;
  29. public bool patrol = false;
  30. public bool kinematicState;
  31. [HideInInspector]
  32. public int effectIdx;
  33. void Start()
  34. {
  35. anim = GetComponentInChildren<Animator>();
  36. if (!isServer) { Destroy(GetComponent<NavMeshAgent>()); return; }
  37. agent = GetComponent<NavMeshAgent>();
  38. t = attackTime;
  39. defaultSpeed = agent.speed;
  40. SetKinematic(true);
  41. StartCoroutine(lookForHoards());
  42. minDistance += Random.Range(-minDistance / 5f, minDistance / 5f);
  43. }
  44. bool chasing = false;
  45. bool higherPlace = false;
  46. // Update is called once per frame
  47. IEnumerator lookForHoards()
  48. {
  49. while (true)
  50. {
  51. zombieBehaviour[] zombies = FindObjectsOfType<zombieBehaviour>();
  52. foreach (zombieBehaviour zombie in zombies)
  53. {
  54. if (Vector3.Distance(transform.position, zombie.transform.position) < minDistance)
  55. {
  56. if (zombie.chasing)
  57. {
  58. chasing = true;
  59. break;
  60. }
  61. }
  62. }
  63. yield return new WaitForSeconds(1);
  64. }
  65. }
  66. void Update()
  67. {
  68. if (!isServer) { return; }
  69. if (player == null)
  70. {
  71. player = m_zombieArea.giveMePlayer();
  72. return;
  73. }
  74. curDistance = Vector3.Distance(transform.position, new Vector3(player.position.x, transform.position.y, player.position.z));
  75. // Debug.Log(player.GetComponent<CharacterController>().isGrounded);
  76. // if (chasing && player != null) { agent.SetDestination(player.position); return; }
  77. float dist = curDistance;
  78. if (dist > agent.stoppingDistance)
  79. {
  80. }
  81. if (dist < minDistance || chasing)
  82. {
  83. if (agent.stoppingDistance > dist || higherPlace)
  84. {
  85. _attack();
  86. }
  87. else
  88. {
  89. t = attackTime;
  90. }
  91. /* else if(anim.GetCurrentAnimatorStateInfo(0).IsName("Attack"))
  92. {
  93. t = 0;
  94. anim.CrossFadeInFixedTime("Idle", 0.1f);
  95. }*/
  96. transform.LookAt(new Vector3(player.position.x, transform.position.y, player.position.z));
  97. // if (anim.GetCurrentAnimatorStateInfo(0).IsName("Attack") || higherPlace) { return; }
  98. NavMeshHit hit;
  99. if (NavMesh.SamplePosition(player.position, out hit, 2.0f, NavMesh.AllAreas))
  100. {
  101. if (!Mathf.Approximately(player.position.y, hit.position.y))
  102. {
  103. // Debug.Log("D:"+Vector3.Distance(transform.position,hit.position));
  104. if (Vector3.Distance(transform.position, hit.position) < 1.5f )
  105. {
  106. Debug.Log("Attack for higher");
  107. higherPlace = true;
  108. }
  109. else
  110. {
  111. higherPlace = false;
  112. }
  113. // _attack();
  114. }
  115. agent.SetDestination(hit.position);
  116. }
  117. chasing = true;
  118. anim.SetBool("moving", (dist > agent.stoppingDistance));
  119. agent.speed = defaultSpeed;
  120. }
  121. else
  122. {
  123. agent.speed = defaultSpeed / 10f;
  124. if (!patrol)
  125. {
  126. agent.destination = transform.position;
  127. anim.SetBool("moving", false);
  128. anim.SetBool("walking", false);
  129. }
  130. else
  131. {/*
  132. anim.SetBool("walking", true);
  133. Transform[] pathObjs = pathParent.GetComponentsInChildren<Transform>();
  134. if (Vector3.Distance(transform.position, pathObjs[curPathIndex].position) > minDistToPath)
  135. {
  136. agent.SetDestination(pathObjs[curPathIndex].position);
  137. }
  138. else
  139. {
  140. if (curPathIndex < pathObjs.Length - 1)
  141. {
  142. curPathIndex++;
  143. }
  144. else
  145. {
  146. curPathIndex = 0;
  147. }
  148. }
  149. */
  150. }
  151. }
  152. }
  153. public void SetKinematic(bool newValue)
  154. {
  155. Rigidbody[] bodies = GetComponentsInChildren<Rigidbody>();
  156. foreach (Rigidbody rb in bodies)
  157. {
  158. rb.isKinematic = newValue;
  159. rb.tag = "Zombie";
  160. // rb.gameObject.layer = (newValue) ? 0 : 11;
  161. }
  162. kinematicState = newValue;
  163. if(agent!=null)agent.enabled = newValue;
  164. }
  165. void _attack()
  166. {
  167. if (t < attackTime) { t += Time.deltaTime; return; }
  168. else
  169. {
  170. anim.CrossFadeInFixedTime("Attack", 0.1f);
  171. StartCoroutine(attack());
  172. Debug.Log("Done damages 2");
  173. t = 0;
  174. }
  175. }
  176. IEnumerator attack()
  177. {
  178. yield return new WaitForSeconds(0.5f);
  179. // if (agent.stoppingDistance > Vector3.Distance(player.position, transform.position))
  180. // {
  181. player.GetComponent<netPlayer>().callDamage(damage);
  182. Debug.Log("Done damages");
  183. // }
  184. }
  185. public zombieManager m_zombieArea;
  186. public void hitboxDamage(HealthEventData damageData, shotType hitboxType)
  187. {
  188. float dmg = 0;
  189. switch (hitboxType)
  190. {
  191. case shotType.headshot:
  192. dmg = damageData.Delta * 5;
  193. break;
  194. case shotType.chestshot:
  195. dmg = damageData.Delta;
  196. break;
  197. case shotType.legshot:
  198. dmg = damageData.Delta / 2f;
  199. break;
  200. }
  201. if (!isServer)
  202. {
  203. CmdDmg(dmg);
  204. }else{
  205. Dmg(dmg);
  206. }
  207. }
  208. [Command(requiresAuthority =false)]
  209. public void CmdDmg(float damage)
  210. {
  211. Dmg(damage);
  212. }
  213. void Dmg(float damage){
  214. health += damage;
  215. chasing = true;
  216. // Debug.Log("shotY : " + damageData.HitPoint.y + " ,chestBottom : " + chestBottom + " ,headBottom" + headBottom);
  217. // Instantiate(bloodPrefab, damageData.HitPoint, Quaternion.Euler(damageData.HitDirection));
  218. if (health < 0)
  219. {
  220. health = 0;
  221. if (m_zombieArea != null)
  222. {
  223. m_zombieArea.currentZombies.Remove(gameObject);
  224. }
  225. Destroy(agent);
  226. die();
  227. RpcDie();
  228. }
  229. }
  230. [ClientRpc]
  231. public void RpcDie()
  232. {
  233. if(isServer){return;}
  234. die();
  235. }
  236. public void die()
  237. {
  238. Collider[] bodies = GetComponentsInChildren<Collider>();
  239. foreach (Collider collider in bodies)
  240. {
  241. collider.gameObject.layer = 6;
  242. }
  243. //anim.CrossFadeInFixedTime("die", 0.1f);
  244. SetKinematic(false);
  245. if (GetComponent<NetworkAnimator>() != null) { Destroy(GetComponent<NetworkAnimator>()); }
  246. Destroy(anim);
  247. Destroy(GetComponent<AudioSource>());
  248. // OnDie.Invoke();
  249. float lastX = 0;
  250. foreach (GameObject go in dropOnDie)
  251. {
  252. go.transform.parent = null;
  253. if (go.GetComponent<Rigidbody>() != null)
  254. {
  255. go.GetComponent<Rigidbody>().velocity = Vector3.zero;
  256. }
  257. go.transform.position += new Vector3(lastX, 0, 0);
  258. lastX += go.transform.lossyScale.x / 2f;
  259. go.SetActive(true);
  260. }
  261. Destroy(this);
  262. }
  263. }
  264. public enum shotType
  265. {
  266. headshot,
  267. chestshot,
  268. legshot
  269. }