Advertisement
kadyr

Untitled

Oct 9th, 2021
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class SniperEnemy : Enemy
  7. {
  8. [SerializeField]
  9. private GameObject bullet;
  10.  
  11. [SerializeField]
  12. private Transform riffleStart;
  13.  
  14. private enum SniperState
  15. {
  16. patrol,
  17. prepare,
  18. fire
  19. }
  20.  
  21. private float timer;
  22. private float coolDown = 5f;
  23.  
  24. [SerializeField]
  25. private float area = 50f;
  26.  
  27. private SniperState states;
  28.  
  29. protected override void Move()
  30. {
  31. if (states == SniperState.patrol)
  32. {
  33. GetComponent<CharacterController>().Move(transform.forward * Time.deltaTime);
  34. if (timer>10)
  35. {
  36. transform.Rotate(new Vector3(0,90,0));
  37. timer = 0;
  38. }
  39. timer += Time.deltaTime;
  40. }
  41. }
  42.  
  43. protected override void Attack()
  44. {
  45. switch(states)
  46. {
  47. case SniperState.patrol:
  48. if (Vector3.Distance(transform.position, player.transform.position) < area)
  49. {
  50. states = SniperState.prepare;
  51. timer = 0;
  52. }
  53. break;
  54. case SniperState.prepare:
  55. timer += Time.deltaTime;
  56. transform.LookAt(player.transform.position);
  57. if (timer >= coolDown)
  58. states = SniperState.fire;
  59. break;
  60. case SniperState.fire:
  61. GameObject buf = Instantiate(bullet);
  62. buf.transform.position = riffleStart.transform.position;
  63. buf.transform.rotation = transform.rotation;
  64. buf.GetComponent<Bullet>().setDirection(transform.forward);
  65. timer = 0;
  66. states = SniperState.patrol;
  67. break;
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement