- Home /
The question is answered, right answer was accepted
Problems with Collision
Hi I'm having probems with the collision on my zombie, here is the code for the collision.
var curHealth : int = 100;
var maxHealth : int = 100;
function OnCollisionEnter(hit : Collision) {
if(hit.transform.name == "ZOMBIE") {
curHealth -= 3;
Debug.Log("ok");
}
}
var healthtext : GUIText;
var regenActive = true;
function Start () {
healthRegen();
}
function Update () {
healthtext.text = curHealth + " / " + maxHealth;
if(curHealth < 0 ) {
Application.LoadLevel("NewOne");
}
if(curHealth > 100) {
curHealth = 100;
}
if(Input.GetKeyDown("e")) {
curHealth -= 101;
}
}
function healthRegen () {
while(regenActive==true) {
yield WaitForSeconds(0.5);
if(curHealth < maxHealth) {
curHealth++;
}
}
}
The zombie has a box collider, but when I touch it my health doesn't go down, do I need change the propertes of the zombie or the actual script?
This question has been asked many, many times. Please confirm that you have already investigated and followed the most common solutions:
Is this script actually attached to your player gameobject in the scene?
Do both parties involved in the collision (i.e. the zombie and the player) have colliders attached?
Are the colliders marked with IsTrigger off?
Does at least one of either the zombie or the player also have a non-kinematic rigidbody component attached?
Is the zombie object called ZO$$anonymous$$BIE in the hierarchy? (And not, for example, ZO$$anonymous$$BIE (Clone), if instantiated from a prefab)
Please also format the code properly, so that it is easier to see what is going on
Sorry, I was looking through other scripts and found the answer. Anyways thank you for your contribution!
Answer by Ayr · Nov 17, 2014 at 10:14 AM
Make sure you attach a rigid body to your object as well if your using Collision be sure to have trigger unchecked on the collider. Sorry c#
GameObject ZOMBIE;
void OnCollisionEnter(Collision collision) {
if(collision.gameObject.name == "ZOMBIE")
{
curHealth -= 3;
Debug.Log ("curHealth");
}
}
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Trying to Disable Compenent when there's a collsion 2 Answers
Detect Collision in an If Statement? 1 Answer
Don't restart music on death 2 Answers