Advertisement
kadyr

Untitled

Aug 24th, 2021
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Turret : MonoBehaviour
  6. {
  7. public GameObject playerObject;
  8. public Transform riffleStart;
  9. public GameObject bullet;
  10.  
  11. [SerializeField] float area = 20f;
  12.  
  13.  
  14. float timer = 0;
  15. float coolDown = 2f;
  16.  
  17. [SerializeField] private Animator enemyAnimator;
  18.  
  19. [SerializeField] private int health = 100;
  20.  
  21. void Start()
  22. {
  23. playerObject = GameObject.FindObjectOfType<PlayerController>().gameObject;
  24. enemyAnimator = GetComponent<Animator>();
  25. }
  26. public void ChangeHealth(int hp)
  27. {
  28. health += hp;
  29. if (health <= 0)
  30. Dead();
  31. }
  32.  
  33. public void Dead()
  34. {
  35. enemyAnimator.SetTrigger("deathTrigger");
  36. area = -1;
  37. }
  38.  
  39. void Update()
  40. {
  41. if (Vector3.Distance(transform.position, playerObject.transform.position) < area)
  42. {
  43. transform.LookAt(playerObject.transform.position);
  44. timer += Time.deltaTime;
  45. if (timer > coolDown)
  46. {
  47. GameObject bul = Instantiate(bullet, riffleStart.transform.position, riffleStart.transform.rotation);
  48. bul.GetComponent<Bullet>().SetDirection(transform.forward);
  49. timer = 0;
  50. }
  51. }
  52. }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement