- Home /
Rigidbody Collider Hero health does not drain when touched by an enemy rigidbody collider.
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;
}
}
}
If this is one of the new 2D games with 2D colliders, then you need the 2D versions of the collision callbacks: OnCollisionEnter2D and OnCollisionStay2D.
^Exactly why I asked! I also believe it have the Collider2D function as well!
Answer by sabin22 · Jan 16, 2014 at 03:54 PM
Thank you everyone for your responses! It turns out I just had to change "Collider" to "Collision" in the functions. I don't think I need to add "2D" because the colliders are 3D.
Your answer
Follow this Question
Related Questions
OnTriggerEnter and OnTriggerExit both trigger twice 0 Answers
C# Invalid Points Assigned to 2D Edge Collider 1 Answer
2D Function analyzing if there is a Gameobject where you click ? 4 Answers
Unity2D - Box Collider 2D problem 0 Answers
Do 2d Colliders cause problems when inside nested objects in Unity? 0 Answers