Files
WildRoot/Assets/Script/BulletSpawner.cs
T

53 lines
1.1 KiB
C#
Raw Normal View History

2026-04-14 17:06:58 +09:00
using UnityEngine;
public class BulletSpawner : MonoBehaviour
{
public GameObject prefab;
public float bulletSpeed = 8f;
Transform target;
2026-04-16 15:04:32 +09:00
public Transform firePoint;
2026-04-14 17:06:58 +09:00
public float min = 0.5f;
public float max = 3f;
float rate;
float time;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
time = 0f;
rate = Random.Range(min, max);
target = FindFirstObjectByType<PlayerController>().transform;
}
// Update is called once per frame
void Update()
{
time += Time.deltaTime;
2026-04-16 15:04:32 +09:00
transform.LookAt(target);
2026-04-14 17:06:58 +09:00
if(time >= rate)
{
time = 0f;
rate = Random.Range(min, max);
2026-04-16 15:04:32 +09:00
GameObject obj = Instantiate(prefab, firePoint.transform.position, firePoint.transform.rotation);
2026-04-14 17:06:58 +09:00
obj.transform.LookAt(target);
Bullet bulletScript = obj.GetComponent<Bullet>();
if (bulletScript != null)
{
bulletScript.speed = bulletSpeed;
}
}
}
}