I am a beginner and use to follow “Brackeys” unity tutorials.
But now i want to damage my player (by 10 ) whenever it touches my enemy.
here’s my health bar script i’m using…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHealth : MonoBehaviour
{
public int maxHealth = 100;
public int currentHealth;
public HealthBar healthBar;
public GameObject deathEffect;
// Use this for initialization
void Start ()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth (maxHealth);
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.LeftControl))
TakeDamage (5);
if (Input.GetKeyDown (KeyCode.Space))
TakeDamage (5);
if (currentHealth <= 0) {
Die ();
}
}
void TakeDamage (int damage)
{
currentHealth -= damage;
healthBar.SetHealth (currentHealth);
}
void Die ()
{
Instantiate (deathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
Thank You in advance.