/* HealthController.cs */ | |
using System.Collections; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class HealthController : MonoBehaviour | |
{ | |
[SerializeField] | |
private GameObject healthPanel; | |
[SerializeField] | |
private RectTransform healthBar; | |
[SerializeField] | |
private Text healthText; | |
private float healthBarStartWidth; | |
private float currentHealth; | |
[SerializeField] | |
private float maxHealth; | |
[SerializeField] | |
private float respawnTime; | |
private MeshRenderer meshRenderer; | |
private bool isDead; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
healthBarStartWidth = healthBar.sizeDelta.x; | |
meshRenderer = GetComponent<MeshRenderer>(); | |
ResetHealth(); | |
UpdateHealthUI(); | |
} | |
public void ApplyDamage(float damage) | |
{ | |
if (isDead) return; | |
currentHealth -= damage; | |
if (currentHealth <= 0) | |
{ | |
currentHealth = 0; | |
isDead = true; | |
meshRenderer.enabled = false; | |
healthPanel.SetActive(false); | |
StartCoroutine(RespawnAfterTime()); | |
} | |
UpdateHealthUI(); | |
} | |
IEnumerator RespawnAfterTime() | |
{ | |
yield return new WaitForSeconds(respawnTime); | |
ResetHealth(); | |
} | |
private void ResetHealth() | |
{ | |
isDead = false; | |
currentHealth = maxHealth; | |
meshRenderer.enabled = true; | |
healthPanel.SetActive(true); | |
UpdateHealthUI(); | |
} | |
private void UpdateHealthUI() | |
{ | |
float percentOutOf = (currentHealth / maxHealth) * 100; | |
float newWidth = (percentOutOf/ 100f) * healthBarStartWidth; | |
healthBar.sizeDelta = new Vector2(newWidth, healthBar.sizeDelta.y); | |
healthText.text = currentHealth + "/" + maxHealth; | |
} | |
} | |
/* WeaponController.cs */ | |
using UnityEngine; | |
public class WeaponController : MonoBehaviour | |
{ | |
[SerializeField] | |
private Weapon weapon; | |
[SerializeField] | |
private string enemyTag; | |
private bool fire; | |
void Update() | |
{ | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
fire = true; | |
} | |
if (Input.GetMouseButtonUp(0)) | |
{ | |
fire = false; | |
} | |
if (fire) | |
{ | |
weapon.Fire(enemyTag); | |
if (weapon.UseTap()) | |
{ | |
fire = false; | |
} | |
} | |
} | |
} | |
/* Weapon.cs */ | |
using System.Collections; | |
using UnityEngine; | |
public class Weapon : MonoBehaviour | |
{ | |
[SerializeField] | |
private float damage; | |
[SerializeField] | |
private float fireRate; | |
[SerializeField] | |
private float fireDistance = 10; | |
[SerializeField] | |
private Transform bulletPoint; | |
private RaycastHit hit; | |
private bool cooldown; | |
public void Fire(string enemyTag) | |
{ | |
if (cooldown) return; | |
Ray ray = new Ray(); | |
ray.origin = bulletPoint.position; | |
ray.direction = bulletPoint.TransformDirection(Vector3.forward); | |
Debug.DrawRay(ray.origin, ray.direction*fireDistance, Color.green); | |
if (Physics.Raycast(ray, out hit, fireDistance)) | |
{ | |
if (hit.collider.CompareTag(enemyTag)) | |
{ | |
var healthCtrl = hit.collider.GetComponent<HealthController>(); | |
healthCtrl.ApplyDamage(damage); | |
} | |
} | |
cooldown = true; | |
StartCoroutine(StopCooldownAfterTime()); | |
} | |
private IEnumerator StopCooldownAfterTime() | |
{ | |
yield return new WaitForSeconds(fireRate); | |
cooldown = false; | |
} | |
public bool UseTap() | |
{ | |
return fireRate == 0; | |
} | |
} | |
/* Movement.cs */ | |
using UnityEngine; | |
public class Movement : MonoBehaviour | |
{ | |
private Rigidbody rigidbody; | |
private float speed = 10; | |
void Start () | |
{ | |
rigidbody = GetComponent<Rigidbody>(); | |
} | |
void Update() | |
{ | |
float h = Input.GetAxis("Horizontal"); | |
float v = Input.GetAxis("Vertical"); | |
Vector3 velocity = (new Vector3(h, 0, v) * speed)-rigidbody.velocity; | |
rigidbody.AddForce(velocity); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment