This week I made some big steps forward by adding enemies with health bars, a way to damage them, and a trap system that activates when the player steps on a pressure plate. Here’s a breakdown of what I built, how it works, and some snippets of the code I wrote.
Adding Enemies with Health Bars
I wanted my enemies to have visible health that goes down when they’re hit. To do this, I created an EnemyHealth script and attached a UI health bar to each enemy.
EnemyHealth.cs
This script keeps track of the enemy’s health, updates the health bar, and destroys the enemy when their health reaches zero.
public class EnemyHealth : MonoBehaviour
{
public float maxHealth = 100f;
public Image healthFill; // Assign this to the fill part of the UI health bar
float currentHealth;
void Start()
{
currentHealth = maxHealth;
UpdateHealthBar();
}
public void DamageTaken(float damage)
{
currentHealth -= damage;
currentHealth = Mathf.Max(currentHealth, 0);
UpdateHealthBar();
if (currentHealth <= 0)
{
Destroy(gameObject); // Remove enemy from the scene
}
}
void UpdateHealthBar()
{
healthFill.fillAmount = currentHealth / maxHealth;
}
}
WeaponDamage.cs
To actually deal damage, I made a WeaponDamage script. This script checks if the weapon hits something tagged as “Enemy” and, if so, calls the enemy’s DamageTaken method.
public class WeaponDamage : MonoBehaviour
{
public string weaponType;
public float damage;
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
EnemyHealth enemy = collision.gameObject.GetComponent<EnemyHealth>();
if (enemy != null)
{
enemy.DamageTaken(damage);
}
}
}
}
Trap System: Pressure Plate Activated Spikes
To make the environment more interactive, I added a trap system where spikes shoot out when the player steps on a pressure plate.
SpikeScript.cs
This script makes the spikes move forward and destroys them after a set time.
public class SpikeScript : MonoBehaviour
{
public float speed = 3f;
public float lifetime = 5f;
private void Start()
{
Destroy(gameObject, lifetime); // Destroy spike after 'lifetime' seconds
}
private void Update()
{
transform.position += Vector3.forward * speed * Time.deltaTime;
}
}
TrapSystem.cs
This script spawns spikes when the player steps on the pressure plate.
public class TrapSystem : MonoBehaviour
{
public GameObject trapPrefab; // Assign your spike prefab here
public Transform spawnPoint; // Where the spikes will spawn
public int trapCount = 3; // How many spikes to spawn
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
for (int i = 0; i < trapCount; i++)
{
Vector3 offset = new Vector3(0, 0, i * 2f);
Instantiate(trapPrefab, spawnPoint.position + offset, Quaternion.identity);
}
}
}
}
What I Learned
- UI in World Space: I had to set the health bar canvas to “World Space” and make sure it always faced the player for better visibility.
- Tag Management: Using tags like “Enemy” and “Player” helped keep my collision checks clean and efficient.
- Prefab Power: By using prefabs for both enemies and traps, I can easily reuse and tweak them throughout my level.
Next Steps
- Make traps deal damage to the player.
- Add sound and visual effects for when enemies are hit or traps are triggered.
- Improve enemy AI so they can move or attack.
- Balance weapon and trap damage for better gameplay.
This week’s progress made my VR world feel much more alive and interactive! If you want to see any specific code or have questions about how something works, let me know in the comments!