Files
ClearTheZone/Assets/Scripts/EnemyShooter.cs
T
2026-08-20 11:31:38 +02:00

47 lines
1.2 KiB
C#

using UnityEngine;
public class EnemyShooter : MonoBehaviour
{
public float shootRange = 25f;
public float shootDelay = 1f;
public int damage = 10;
private PlayerHealth player;
private float nextShotTime;
void Start()
{
player = FindFirstObjectByType<PlayerHealth>();
}
void Update()
{
if (PauseMenu.GameIsPaused || player == null) return;
Vector3 direction = player.transform.position - transform.position;
if (direction.magnitude > shootRange) return;
direction.y = 0f;
if (direction != Vector3.zero)
transform.rotation = Quaternion.LookRotation(direction);
if (Time.time >= nextShotTime && CanSeePlayer())
{
player.TakeDamage(damage);
nextShotTime = Time.time + shootDelay;
}
}
bool CanSeePlayer()
{
Vector3 start = transform.position + Vector3.up;
Vector3 target = player.transform.position + Vector3.up;
Vector3 direction = target - start;
if (Physics.Raycast(start, direction.normalized, out RaycastHit hit, shootRange))
return hit.collider.GetComponentInParent<PlayerHealth>() != null;
return false;
}
}