Advertisement
Guest User

Boris

a guest
May 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.23 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.AI;
  6.  
  7.  
  8. public class Boris : MonoBehaviour
  9. {
  10. public float speed = 1;
  11. public Transform[] moveSpots;
  12. private int randomSpots;
  13.  
  14. private float waitTime;
  15. private bool setWait;
  16. public float startWaitTime = 0;
  17.  
  18. public float gravity = -12;
  19. public NavMeshAgent navMesh;
  20. public GameObject player;
  21. public Animator animBoris;
  22. public bool isLamabile;
  23. public GameObject boris;
  24. public ParticleSystem blood;
  25. public ParticleSystem bloodBody;
  26.  
  27.  
  28. public bool isPunching;
  29. public float punchTimer;
  30.  
  31. public float life;
  32. public bool isDead;
  33. public bool killOk;
  34.  
  35.  
  36. public AudioSource borisPunchSound;
  37.  
  38. public GameObject zonaLama;
  39.  
  40. public AudioSource hitSound;
  41.  
  42. public bool bodyHit;
  43. public bool headHit;
  44.  
  45. public float cadenzaPunch = 1f;
  46.  
  47.  
  48.  
  49. // Start is called before the first frame update
  50. void Start()
  51. {
  52. /*------------------------
  53. * inizializzo la navmesh
  54. * -----------------------*/
  55. navMesh = GetComponent<NavMeshAgent>();
  56. animBoris = transform.GetComponent<Animator>();
  57. navMesh.speed = speed;
  58. navMesh.autoBraking = false;
  59. waitTime = startWaitTime;
  60. setWait = false;
  61. navMesh.SetDestination(moveSpots[0].position);
  62. navMesh.stoppingDistance = 0;
  63. navMesh.updateRotation = false;
  64. isLamabile = false;
  65. isPunching = false;
  66. isDead = false;
  67. killOk = false;
  68. //fuoco.enableEmission = false;
  69. life = 100f;
  70.  
  71. zonaLama = gameObject.transform.GetChild(0).gameObject;
  72.  
  73. player = GameObject.FindGameObjectWithTag("Player");
  74. animBoris = navMesh.gameObject.GetComponentInChildren<Animator>();
  75. animBoris.SetBool("isWalking", true);
  76. animBoris.SetBool("playerContact", false);
  77. animBoris.SetFloat("speedPercentage", 1);
  78. /*------------------------
  79. * seleziono a caso il primo punto del giro di pattuglia
  80. * -----------------------*/
  81. randomSpots = Random.Range(0, moveSpots.Length);
  82.  
  83. bodyHit = false;
  84.  
  85. }
  86.  
  87. private void Update()
  88. {
  89. if (life == 0)
  90. isDead = true;
  91. if (CharacterControllerScript.player_contact)
  92. if (navMesh.velocity != Vector3.zero)
  93. {
  94. animBoris.SetFloat("speedPercentageC", 1);
  95. }
  96. else
  97. {
  98. //animBoris.SetFloat("speedPercentageC", 0);
  99. }
  100. else
  101. if (navMesh.velocity != Vector3.zero)
  102. {
  103. animBoris.SetFloat("speedPercentage", 1);
  104. }
  105. else
  106. {
  107. animBoris.SetFloat("speedPercentage", 0);
  108. }
  109.  
  110.  
  111.  
  112. }
  113.  
  114. // Update is called once per frame
  115. void LateUpdate()
  116. {
  117. if (isDead && !killOk)
  118. {
  119. kill();
  120. }
  121.  
  122.  
  123. if (!CharacterControllerScript.player_contact || isDead)
  124. {
  125. /*------------------------
  126. * se boris non ci vede o è morto
  127. * -----------------------*/
  128. if (CharacterControllerScript.player_contact_deactivated)
  129. {
  130. //se il player è sfuggito
  131. navMesh.SetDestination(moveSpots[randomSpots].position);
  132. waitTime = 0;
  133. animBoris.SetBool("playerContact", false);
  134. }
  135.  
  136.  
  137. if ((!navMesh.pathPending && navMesh.remainingDistance < 1f)|| isDead)
  138. {
  139.  
  140. /*------------------------
  141. * se è in posizione
  142. * -----------------------*/
  143. if (!setWait)
  144. {
  145. setWait = true;
  146. waitTime = startWaitTime;
  147. }
  148.  
  149. if (waitTime <= 0)
  150. {
  151. /*------------------------
  152. * vai alla prossima posizione
  153. * -----------------------*/
  154.  
  155. if (moveSpots.Length == 0)
  156. {
  157. randomSpots = 0;
  158. }
  159. else
  160. {
  161. randomSpots = Random.Range(0, moveSpots.Length);
  162. }
  163. navMesh.SetDestination(moveSpots[randomSpots].position);
  164. navMesh.speed = 1;
  165. animBoris.SetFloat("speedPercentage", 0);
  166. setWait = false;
  167.  
  168. }
  169. else
  170. {
  171. /*------------------------
  172. * aspetta nel waypoint
  173. * -----------------------*/
  174. waitTime -= 0.001f;
  175. navMesh.speed = 0;
  176. animBoris.SetFloat("speedPercentage", 0);
  177. //qua deve guardarsi attorno
  178. }
  179.  
  180. }
  181. else
  182. {
  183. /*------------------------
  184. * cambio di posizione
  185. * -----------------------*/
  186. navMesh.destination = moveSpots[randomSpots].position;
  187. if (navMesh.velocity.sqrMagnitude > Mathf.Epsilon)
  188. {
  189. transform.rotation = Quaternion.LookRotation(navMesh.velocity.normalized);
  190. }
  191. //navMesh.transform.LookAt(moveSpots[randomSpots].position);
  192. }
  193.  
  194. }
  195. else
  196. {
  197.  
  198. /*------------------------
  199. * se siamo stati visti dal nemico
  200. * -----------------------*/
  201. animBoris.SetBool("playerContact", true);
  202. navMesh.destination = player.transform.position;
  203. transform.LookAt(player.transform.position + (new Vector3(0, 1f, 0)));
  204. navMesh.stoppingDistance = 1f;
  205. navMesh.speed = 4;
  206. if (navMesh.remainingDistance < 1.5f)
  207. {
  208. //se è abbastanza vicino picchia
  209. punchOnPlayer();
  210.  
  211. }
  212. if (navMesh.speed == 0 || navMesh.remainingDistance < 1.5f)
  213. {
  214. // animBoris.SetBool("isWalking", false);
  215.  
  216.  
  217. animBoris.SetFloat("speedPercentageC", 0);
  218. }
  219. else
  220. {
  221. // animBoris.SetBool("isWalking", true);
  222. animBoris.speed = 1;
  223. animBoris.SetBool("isPunching", false);
  224. animBoris.SetBool("isKicking", false);
  225. animBoris.SetFloat("speedPercentageC", 1);
  226. }
  227. }
  228.  
  229. if (headHit)
  230. {
  231. blood.Play();
  232. if (life > 0)
  233. {
  234. decrLife(100);
  235. }
  236. headHit = false;
  237.  
  238. }
  239.  
  240. if (bodyHit)
  241. {
  242. bloodBody.Play();
  243. if (life > 0)
  244. {
  245. if (CharacterControllerScript.specialBullet)
  246. {
  247. decrLife(34 * 3);
  248. }
  249. else
  250. {
  251. decrLife(34);
  252. }
  253. if (life > 0)
  254. {
  255. CharacterControllerScript.player_contact = true;
  256. hitSound.Play();
  257. }
  258.  
  259. }
  260. bodyHit = false;
  261.  
  262. }
  263.  
  264. }
  265.  
  266. void punchOnPlayer()
  267. {
  268. RaycastHit hit;
  269. Vector3 fucile = navMesh.transform.position;
  270. fucile.y += 0.3f;
  271. animBoris.speed = 2;
  272. if (!isPunching && CharacterControllerScript.player_contact)
  273. {
  274. isPunching = true;
  275. punchTimer = Random.Range(0, 1.5f);
  276. if (Physics.Raycast(fucile, navMesh.transform.forward, out hit))
  277. {
  278. if (Random.Range(0, 2) % 2 == 0)
  279. {
  280. animBoris.SetBool("isPunching", true);
  281. }
  282. else
  283. {
  284. animBoris.SetBool("isKicking", true);
  285. }
  286. Debug.Log("Boris punch");
  287. Debug.DrawRay(fucile, navMesh.transform.forward * 10, Color.green);
  288. Debug.Log("Boris colpisce: " + hit.collider.gameObject.name);
  289. if (hit.collider.gameObject.tag == "Player")
  290. {
  291. borisPunchSound.Play();
  292. if (!CharacterControllerScript.immortality)
  293. {
  294. CharacterControllerScript.decrHealth(6);
  295. if (CharacterControllerScript.isDead)
  296. {
  297. ShowMessage.id = 8;
  298. }
  299. }
  300. CharacterControllerScript.PlayerBlood.Play();
  301. Talk.id = 2;
  302. }
  303. }
  304. }
  305. punchTimer -= Time.deltaTime * cadenzaPunch;
  306. if (punchTimer <= 0 || CharacterControllerScript.isDead)
  307. {
  308. isPunching = false;
  309. animBoris.SetBool("isPunching", false);
  310. animBoris.SetBool("isKicking", false);
  311. animBoris.speed = 1;
  312. }
  313. }
  314.  
  315. public void decrLife(int damage)
  316. {
  317. if (life - damage > 0)
  318. {
  319. life -= damage;
  320. }
  321. else
  322. {
  323. life = 0;
  324. kill();
  325. }
  326.  
  327. }
  328.  
  329. public void incLife(int cura)
  330. {
  331. if (life + cura <= 100)
  332. {
  333. life += cura;
  334. }
  335. else
  336. {
  337. life = 100;
  338. }
  339. }
  340.  
  341. public void kill()
  342. {
  343. animBoris.speed = 1;
  344. ShowMessage.id = 0;
  345. //speed = 0;
  346.  
  347.  
  348.  
  349. //navMesh.enabled = false;
  350. if (!isDead)
  351. {
  352. isDead = true;
  353. killOk = true;
  354. }
  355. animBoris.SetBool("isDead", true);
  356. //Destroy(navMesh);
  357. //Destroy(this);
  358. //Destroy(transform);
  359. }
  360.  
  361.  
  362. public void stopEnemy()
  363. {
  364. this.speed = 0f;
  365. navMesh.isStopped = true;
  366. //animBoris.SetFloat("speedPercentage", 0.1f);
  367. }
  368.  
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement