Advertisement
kadyr

Untitled

Jul 31st, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 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.  
  18. void Start()
  19. {
  20. playerObject = GameObject.FindObjectOfType<PlayerController>().gameObject;
  21. }
  22.  
  23. void Update()
  24. {
  25. if (Vector3.Distance(transform.position, playerObject.transform.position) < area)
  26. {
  27. transform.LookAt(playerObject.transform.position);
  28. timer += Time.deltaTime;
  29. if (timer > coolDown)
  30. {
  31. GameObject bul = Instantiate(bullet, riffleStart.transform.position, riffleStart.transform.rotation);
  32. bul.GetComponent<Bullet>().SetDirection(transform.forward);
  33. timer = 0;
  34. }
  35. }
  36. }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement