Skip to content

Instantly share code, notes, and snippets.

@niiicolai
Last active June 29, 2020 00:43
Show Gist options
  • Save niiicolai/217fef72315338ac60c3c1bfee46a2c4 to your computer and use it in GitHub Desktop.
Save niiicolai/217fef72315338ac60c3c1bfee46a2c4 to your computer and use it in GitHub Desktop.
/* 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;
}
}
/* HealthController.cs */
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class HealthController : MonoBehaviour
{
private bool isDead;
private MeshRenderer meshRenderer;
private float currentHealth;
[SerializeField]
private float maxHealth = 100;
[SerializeField]
private float respawnTime = 5;
[SerializeField]
private Text healthText;
void Start()
{
meshRenderer = GetComponent<MeshRenderer>();
currentHealth = maxHealth;
UpdateHealthUI();
}
public void ApplyDamage(float damage)
{
if (isDead) return;
currentHealth -= damage;
if (currentHealth <= 0)
{
currentHealth = 0;
isDead = true;
meshRenderer.enabled = false;
StartCoroutine(RespawnAfterTime());
}
UpdateHealthUI();
}
private void ResetHealth()
{
isDead = false;
meshRenderer.enabled = true;
currentHealth = maxHealth;
UpdateHealthUI();
}
private IEnumerator RespawnAfterTime()
{
yield return new WaitForSeconds(respawnTime);
ResetHealth();
}
private void UpdateHealthUI()
{
if (healthText != null) healthText.text = currentHealth + "/" + maxHealth;
}
}
/* 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