Advertisement
kadyr

Untitled

Oct 31st, 2021
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Turret : MonoBehaviour
  6. {
  7.     [SerializeField] GameObject player;
  8.     [SerializeField] GameObject bullet;
  9.     [SerializeField] GameObject rifleStart;
  10.  
  11.     [SerializeField]
  12.     float area = 100;
  13.  
  14.     float timer = 0;
  15.     float cooldown = 1;
  16.  
  17.     // Start is called before the first frame update
  18.     void Start()
  19.     {
  20.         player = FindObjectOfType<PlayerController>().gameObject; //Находим игрока
  21.     }
  22.  
  23.     // Update is called once per frame
  24.     void Update()
  25.     {
  26.         if (Vector3.Distance(transform.position, player.transform.position) < area)
  27.         {
  28.             transform.LookAt(player.transform);
  29.             timer += Time.deltaTime;
  30.             if (timer > cooldown)
  31.             {
  32.                 timer = 0;
  33.                 GameObject buf = Instantiate(bullet,rifleStart.transform.position,transform.rotation);
  34.                 buf.GetComponent<Bullet>().SetDirection(transform.forward);
  35.             }
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement