mirror of
https://github.com/ryzij/Cube-Tower-Defense.git
synced 2026-07-14 01:56:57 +00:00
45 lines
1.2 KiB
C#
45 lines
1.2 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
public class Turret : MonoBehaviour
|
|
{
|
|
[SerializeField] private TurretBullet _bulletPrefab;
|
|
[SerializeField] private float _reloadTime = 1f;
|
|
[SerializeField] private float _damage;
|
|
[SerializeField] private float _distance;
|
|
[SerializeField] private int _price;
|
|
|
|
private float _reloadTimer;
|
|
|
|
public int Price => _price;
|
|
|
|
private void Update()
|
|
{
|
|
if (_reloadTimer >= _reloadTime)
|
|
{
|
|
SpawnBullet(Vector3.forward);
|
|
SpawnBullet(Vector3.back);
|
|
SpawnBullet(Vector3.left);
|
|
SpawnBullet(Vector3.right);
|
|
|
|
_reloadTimer = 0f;
|
|
}
|
|
|
|
_reloadTimer += Time.deltaTime;
|
|
}
|
|
|
|
private void SpawnBullet(Vector3 direction)
|
|
{
|
|
if (!DetectEnemy(direction))
|
|
return;
|
|
|
|
var rot = transform.rotation;
|
|
rot.SetLookRotation(direction);
|
|
Instantiate(_bulletPrefab, transform.position, rot, transform);
|
|
}
|
|
|
|
private bool DetectEnemy(Vector3 direction) =>
|
|
Physics.Raycast(transform.position, direction, out var hit, _bulletPrefab.Distance + _distance) &&
|
|
hit.transform.TryGetComponent(out Enemy _);
|
|
}
|