Advertisement
kadyr

Untitled

Sep 12th, 2021
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class SniperEnemy : Enemy
  4. {
  5.     [SerializeField]
  6.     GameObject bullet;
  7.  
  8.     [SerializeField]
  9.     GameObject rifleStart;
  10.  
  11.     [SerializeField]
  12.     float cooldown = 1f;
  13.  
  14.     float timer = 0;
  15.     float area = 25;
  16.  
  17.     private enum SniperState
  18.     {
  19.         Wait,
  20.         Prepare,
  21.         Shoot
  22.     }
  23.  
  24.     private SniperState _currentState = SniperState.Wait;
  25.  
  26.     void Start() { }
  27.  
  28.     public override void Move()
  29.     {
  30.         if (_currentState == SniperState.Wait)
  31.         {
  32.             GetComponent<CharacterController>().Move(transform.forward * Time.deltaTime * 2f);
  33.             timer += Time.deltaTime;
  34.             if (timer > 10)
  35.             {
  36.                 transform.Rotate(new Vector3(0,90,0));
  37.                 timer = 0;
  38.             }
  39.         }
  40.     }
  41.  
  42.     public override void Attack()
  43.     {
  44.         if (Vector3.Distance(transform.position, player.transform.position) < area)
  45.         {
  46.             transform.LookAt(player.transform);
  47.  
  48.             timer += Time.deltaTime;
  49.             if (timer > cooldown)
  50.             {
  51.                 timer = 0;
  52.                 GameObject buf = Instantiate(bullet);
  53.                 buf.transform.position = rifleStart.transform.position;
  54.                 buf.transform.rotation = transform.rotation;
  55.                 buf.GetComponent<Bullet>().setDirection(transform.forward);
  56.             }
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement