Advertisement
kadyr

SniperEnemy

Sep 12th, 2021
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.20 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3.  
  4. public class SniperEnemy : Enemy
  5. {
  6.     [SerializeField]
  7.     GameObject bullet;
  8.  
  9.     [SerializeField]
  10.     GameObject rifleStart;
  11.  
  12.     [SerializeField]
  13.     float cooldown = 1f;
  14.  
  15.     float timer = 0;
  16.     [SerializeField]float area = 25;
  17.  
  18.     private enum SniperState
  19.     {
  20.         Wait,
  21.         Prepare,
  22.         Shoot
  23.     }
  24.  
  25.     private SniperState _currentState = SniperState.Wait;
  26.  
  27.     void Start() { }
  28.  
  29.     public override void Move()
  30.     {
  31.         if (_currentState == SniperState.Wait)
  32.         {
  33.             GetComponent<CharacterController>().Move(transform.forward * Time.deltaTime * 2f);
  34.             timer += Time.deltaTime;
  35.             if (timer > 10)
  36.             {
  37.                 transform.Rotate(new Vector3(0, 90, 0));
  38.                 timer = 0;
  39.             }
  40.         }
  41.     }
  42.  
  43.     public override void Attack()
  44.     {
  45.         switch (_currentState)
  46.         {
  47.             case SniperState.Wait:
  48.                 if (Vector3.Distance(transform.position, player.transform.position) <= area)
  49.                 {
  50.                     _currentState = 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.                 {
  59.                     _currentState = SniperState.Shoot;
  60.                     timer = 0;
  61.                 }
  62.                 if (Vector3.Distance(transform.position, player.transform.position) > area)
  63.                 {
  64.                     _currentState = SniperState.Wait;
  65.                 }
  66.                     break;
  67.             case SniperState.Shoot:
  68.                 timer = 0;
  69.                 GameObject buf = Instantiate(bullet);
  70.                 buf.transform.position = rifleStart.transform.position;
  71.                 buf.transform.rotation = transform.rotation;
  72.                 buf.GetComponent<Bullet>().setDirection(transform.forward);
  73.                 buf.GetComponent<Bullet>().MakeSniper();
  74.                 _currentState = SniperState.Prepare;
  75.                 break;
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement