Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/* Weapon.cs */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
[SerializeField]
private new ParticleSystem particleSystem;
[SerializeField]
private float damage;
[SerializeField]
private float fireRate;
private List<ParticleCollisionEvent> collisionEvents;
private string enemyTag;
private bool fireCooldown;
void Start()
{
collisionEvents = new List<ParticleCollisionEvent>();
}
void OnParticleCollision(GameObject other)
{
ParticlePhysicsExtensions.GetCollisionEvents(particleSystem, other, collisionEvents);
for (int i = 0; i < collisionEvents.Count; i++)
{
var collider = collisionEvents[i].colliderComponent;
if (collider.CompareTag(enemyTag))
{
var health = collider.GetComponent<HealthController>();
health.ApplyDamage(damage);
}
}
}
public void Fire()
{
if (fireCooldown) return;
fireCooldown = true;
particleSystem.Emit(1);
StartCoroutine(StopCooldownAfterTime());
}
private IEnumerator StopCooldownAfterTime()
{
yield return new WaitForSeconds(fireRate);
fireCooldown = false;
}
public void SetEnemyTag(string enemyTag)
{
this.enemyTag = enemyTag;
}
}
/* WeaponController.cs */
using UnityEngine;
public class WeaponController : MonoBehaviour
{
[SerializeField]
private Weapon weapon;
[SerializeField]
private string enemyTag;
private bool fire;
void Start()
{
weapon.SetEnemyTag(enemyTag);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
fire = true;
}
if (Input.GetMouseButtonUp(0))
{
fire = false;
}
if (fire)
{
weapon.Fire();
}
}
}
/* HealthController.cs */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthController : MonoBehaviour
{
[SerializeField]
private float maxHealth;
private float currentHealth;
[SerializeField]
private float respawnTime;
[SerializeField]
private GameObject healthPanel;
[SerializeField]
private Text healthText;
[SerializeField]
private RectTransform healthBar;
private float healthBarStartWidth;
private MeshRenderer meshRenderer;
private bool isDead;
void Start()
{
meshRenderer = GetComponent<MeshRenderer>();
currentHealth = maxHealth;
healthBarStartWidth = healthBar.sizeDelta.x;
UpdateUI();
}
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());
}
UpdateUI();
}
private IEnumerator RespawnAfterTime()
{
yield return new WaitForSeconds(respawnTime);
ResetHealth();
}
private void ResetHealth()
{
currentHealth = maxHealth;
isDead = false;
meshRenderer.enabled = true;
healthPanel.SetActive(true);
UpdateUI();
}
private void UpdateUI()
{
float percentOutOf = (currentHealth / maxHealth) * 100;
float newWidth = (percentOutOf / 100) * healthBarStartWidth;
healthBar.sizeDelta = new Vector2(newWidth, healthBar.sizeDelta.y);
healthText.text = currentHealth + "/" + maxHealth;
}
}
/* Movement.cs */
using UnityEngine;
public class Movement : MonoBehaviour
{
private Rigidbody rigidbody;
[SerializeField]
private float speed;
[SerializeField]
private ForceMode forceMode;
// Start is called before the first frame update
void Start()
{
rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 velocity = new Vector3(0, 0, v);
velocity *= speed;
velocity -= rigidbody.velocity;
rigidbody.AddForce(velocity, forceMode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.