Hi guys, was wondering if you could help me figure my problem out.
I’m trying to create a health script that subtracts health when colliding with an object with the “Enemy Tag”. The script seems to work one every 20 hits… Completely random though. I was wondering if there is anything I can do to my code to either optimise it or ensure that the collision is picked up every time.
I really do thank you for your time and effort to read into my post!
EDIT: The other box I’m colliding into has a rigid body and a box collider.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Health : MonoBehaviour
{
public Image EmptyHealthBar_Image;
public Image FullHealthBar_Image;
public int playerFullHealth = 100;
public int currentPlayerHealth = 100;
//When the player has a collision with another object
public void OnControlColliderHit(Collision collision)
{
//Check the tag
switch(collision.gameObject.tag)
{
case "Enemy":
if(currentPlayerHealth > 0)
{
//Subtract health and update healthbar image
currentPlayerHealth -= 10;
UpdateHealthBar(currentPlayerHealth);
}
break;
case "HealthPack":
if(currentPlayerHealth < playerFullHealth)
{
//Add health and update healthbar image
currentPlayerHealth +=10;
UpdateHealthBar(currentPlayerHealth);
}
break;
default:
break;
}
}
//Updates the healthbar image to reflect added or subtracted health
public void UpdateHealthBar (float currentPlayerHealth)
{
FullHealthBar_Image.fillAmount = currentPlayerHealth / playerFullHealth;
}
}