- Home /
Lose and gain health when collision happens?
So I have two blocks. One depletes the player's health and the other one replenishes it. I have looked around for help on doing just that when I touch the blocks with my character, but am at a standstill. I have put print code inside my function to test if it is even going through, but it isn't. Could you help me out here?
Code:
#pragma strict
var health : float = 100;
var isBad : boolean = false;
function OnCollisionEnter(collision : Collision)
{
if (collision.gameObject.CompareTag("Player"))
{
if(isBad == true)
{
health -= .5;
print("Lose");
}
else if (isBad == false)
{
health += .5;
print ("Gain");
}
}
}
function OnGUI()
{
GUI.Label(Rect(10,10,100,20),health.ToString());
}
1) Did you assign this script to an object with a Collider component? 2) Did you check the button isTrigger? If so, you will need to write OnTriggerEnter function. 3) Did you make sure your character is tagged as Player?
I have checked all three and they are all as they should be. It is assigned to both blocks, isTrigger is not checked. $$anonymous$$y character is definitely tagged as player.
I'd suggest to make change the collider to trigger, 1) check isTrigger, 2) Change the code to OnTriggerEnter and see if it works.
Is my answer the same as yours? If so, it did work :) I changed the code to use OnTriggerEnter as well.
Answer by caleb_b · Jun 17, 2014 at 08:23 PM
I took the liberty of recreating your project as you described it here. I made a slight change you your code, and it worked for me. Here is the new code
#pragma strict
var health : float = 100;
var isBad : boolean = false;
function OnTriggerEnter(other : Collider)
{
if (other.tag == "Player")
{
if(isBad == true)
{
health -= .5;
print("Lose");
}
else if (isBad == false)
{
health += .5;
print ("Gain");
}
}
}
function OnGUI()
{
GUI.Label(Rect(10,10,100,20),health.ToString());
}
I changed your OnCollisionEnter() to OnTriggerEnter() and changed the check for player. When I tried it, the GUI looked a little wacky, but that's outside of the scope of your question. To add this, you'll just need to mark your boxes as trigger, and you should be golden.
I might have been unclear about this but to change the collider to a trigger was suggested in my post, However it was good that you actually tested it to see if it worked :)
Now that I re-read your answer, I see that. I thought it a good idea to check my answer before submitting. I hate when people give me code as an answer to a question, and it's full of errors, as I'm not experienced enough to work the errors out for myself. Thanks for the upvote BTW :)
Answer by multinfs · Jun 17, 2014 at 07:31 PM
First you might have to add a rigidbody to both objects(if you do not already have) and then if you are using the collider as a Trigger if it shouldn't collide physically with other bodies, in that case you need to change function to OnTriggerEnter And change is Trigger to true on the collider.
One last thing I'd try out is to change the tag comparison to something like this:
if (collision.gameObject.tag == "Player")
Just to make sure that isn't the problem.
One note too is that OnCollisionEnter fires only once when a body enters the collider, use OnCollisionStay or OnTriggerStay for continuous fire of the function.
I have made sure to change my if statement to what you suggested and that was not the problem. Everything Tanshaydar suggested I have done as well, with no avail of course.
Your answer
Follow this Question
Related Questions
Creating a Asteroid health script 5 Answers
For loop on collision (two hit kill) 2 Answers
Enemy health not reducing 1 Answer
Enemy health not reducing on collision with bullet 0 Answers
Health Script/Targeting system c# 2 Answers