Advertisement
kadyr

Untitled

Sep 12th, 2021
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 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.     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.                 if (timer > cooldown)
  56.                 {
  57.                     _currentState = SniperState.Shoot;
  58.                 }
  59.                 break;
  60.             case SniperState.Shoot:
  61.                 timer = 0;
  62.                 GameObject buf = Instantiate(bullet);
  63.                 buf.transform.position = rifleStart.transform.position;
  64.                 buf.transform.rotation = transform.rotation;
  65.                 buf.GetComponent<Bullet>().setDirection(transform.forward);
  66.                 break;
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement