UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"create a tower base class for a tower defense game"
public class Tower : MonoBehaviour { public int cost; public int damage; public float range; public float fireRate; public GameObject bulletPrefab; public Transform firePoint; private float fireCountdown = 0f; public virtual void Update() { if (fireCountdown <= 0f) { Shoot(); fireCountdown = 1f / fireRate; } fireCountdown -= Time.deltaTime; } public virtual void Shoot() { GameObject bulletGO = (GameObject)Instantiate(bulletPrefab, firePoint.position, firePoint.rotation); Bullet bullet = bulletGO.GetComponent<Bullet>(); if (bullet != null) { bullet.Seek(); } } }