This has been a hair puller for a few days now. I’m having trouble getting my hero’s health to drain when an enemy touches him in my 2D platformer. Both are rigid bodies, both have colliders, and neither are triggers or kinematic. The enemy pushes the hero, so I know the colliders are working, but the hero’s health does not decrease when touched. I can only assume it has something to do with my code, which I’ve tinkered with to no avail.
Any help would be much appreciated!
var explosion : GameObject;
var explosionSound : GameObject;
var BlastenHealth : float;
var damageRateTouch : float = 1f;
var hitByEmemySFX : GameObject;
private var RateCount : float = 0.0f;
private var ResetCount : float = 0.0f;
private var TriggerEffectsOnce : boolean = true;
// Blasten explodes and respawns after 3 seconds
function Update () {
Debug.Log(BlastenHealth);
if (BlastenHealth <= 0){
if(TriggerEffectsOnce){
Instantiate(explosion, transform.position, transform.rotation);
Instantiate(explosionSound, transform.position, transform.rotation);
TriggerEffectsOnce = false;
}
GetComponent(MeshRenderer).enabled = false;
GetComponent(CharacterController).enabled = false;
ResetCount += Time.deltaTime;
if(ResetCount >= 3){
Application.LoadLevel("SandStage");
}
}
barDisplay = BlastenHealth * 0.01;
}
// Different types of damages to Blasten and their effects
function OnCollisionEnter (col : Collider){
if (col.gameObject.tag =="SmallEnemyCollision") {
BlastenHealth = BlastenHealth - 10;
Instantiate(hitByEmemySFX, transform.position, transform.rotation);
}
if (col.gameObject.tag =="BigEnemyCollision") {
BlastenHealth = BlastenHealth - 15;
Instantiate(hitByEmemySFX, transform.position, transform.rotation);
}
if (col.gameObject.tag =="Small Projectile") {
BlastenHealth = BlastenHealth - 5;
Instantiate(hitByEmemySFX, transform.position, transform.rotation);
}
}
function OnCollisionStay (col : Collider) {
if (col.gameObject.tag =="SmallEnemyCollision") {
RateCount += Time.deltaTime;
if (RateCount >= damageRateTouch){
BlastenHealth = BlastenHealth - 10;
Instantiate(hitByEmemySFX, transform.position, transform.rotation);
RateCount = 0.0f;
}
}
}