123/Assets/Scripts/BulletSpawner.cs

37 lines
919 B
C#
Raw Normal View History

2026-01-27 01:41:07 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletSpawner : MonoBehaviour
{
public GameObject bulletPrefab;
public float spawnRateMin = 0.5f;
public float spawnRateMax = 0.3f;
private Transform target;
private float spawnRate;
private float timeAfterSpawn;
void Start()
{
timeAfterSpawn = 0f;
spawnRate = Random.Range(spawnRateMin, spawnRateMax);
target = FindFirstObjectByType<PlayerController>().transform;
}
void Update()
{
timeAfterSpawn += Time.deltaTime;
if (timeAfterSpawn >= spawnRate)
{
timeAfterSpawn = 0f;
GameObject bullet
= Instantiate(bulletPrefab, transform.position, transform.rotation);
bullet.transform.LookAt(target);
spawnRate = Random.Range(spawnRateMin, spawnRateMax);
}
}
}