- Home /
Decrase value on collision C#
I got a Health Script from BurgZergArcade. And I need something that decrase the curHealth when the enemy (with tag "Enemy") will touch me, for example -10. Also, some delay between decrasing curHealth would be nice c:
Health Script:
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public int maxHealth = 100; //maximum HP
public int curHealth = 100; //current HP
public float healthBarLength;
// Use this for initialization
void Start () {
healthBarLength = Screen.width / 2;
}
// Update is called once per frame
void Update () {
AddjustCurrentHealth (0);
}
void OnGUI(){
GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if (curHealth > maxHealth)
maxHealth = 1;
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
Ins$$anonymous$$d of asking for someone else to do it for you, it's important that you try to learn how to do these things for yourself. It's only going to make it harder and slower in the long run if you don't. There are TONES of answers for how to do something on a collision event - use the search function to find one and go play with it.
The point of this site is to help you learn, not do it for you (though that certainly happens). At least have a go at it first, then come to us with your specific problem when you get stuck.
I know about it, man! I'm actually trying to make it work for about 2 hours. Sadly, I'm newbie and this is hard. $$anonymous$$y spcific problem? It's not working ("Ba Dum Pss" in the background).
Ok, so put up what you've got so it can be corrected etc. What have you got working so far? What are you stuck on?
Is this your first project? Have you done tutorials first?
Yes and Yes.
I was trying:
void OnTriggerEnter(Collider col){
//Things happens here
}
And:
void OnCollisionEnter(Collision collision) { //Things happens here }
But it's still not working properly. I'm better in JavaScript.
You have one of your colliders set to trigger and a rigid body on your object(s)?
Answer by Kryptos · Aug 22, 2012 at 02:08 PM
Collision detection is done (in script) using events sent by Unity engine. For example OnCollisionEnter is sent to the GameObject with a Collider component attached or to the parent Rigidbody when using compound colliders.
Once you know that, you can try a dummy solution like:
void OnCollisionEnter(Collision collision)
{
curHealth -= 10;
}
Of course, you may now want to check the kind of object that you collided with. In order to do that, you can check the name or the tag of the collided object.
String tag = collision.gameObject.tag;
if (tag == "Enemy")
And finally, to make sure that you decrease the value nex timte only after a certain amount of time, you need to use the so-called Time class.
Each time you change the value, you can store the time that event occured:
lastChangeTime = Time.time;
Next attempt to change the value, you check the time elapsed since the previous change:
if (Time.time - lastChangeTime > aCertainAmountOfTime)
{
// change the value
AdjustHealth(-10);
// save the 'last change time'
lastChangeTime = Time.time;
}
With all that, I'm sure you can write your own solution.
Thanks a lot! I'll try to mix this to one working script :)
Answer by Stardog · Aug 22, 2012 at 02:08 PM
You are looking for OnCollisionEnter
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Enemy")
{
AdjustCurrentHealth(-10);
}
}
Also, you will need a rigidbody attached (it's a physics collision). And remove the AdjustCurrentHealth from Update.
For a time delay:
IEnumerator AdjustCurrentHealth(int adj)
{
yield return new WaitForSeconds(5);
...
Then StartCoroutine(AdjustCurrentHealth(-10)); where it was before.
StartCoroutine won't solve the issue here. You're just delaying the change but you don't prevent the coroutine from being called more than once during this delay.
Your answer
Follow this Question
Related Questions
My Photon health script doesnt work 2 Answers
(C#) Enemy health and take damage from bullets 1 Answer
Photon Combat script does not work 0 Answers
Take health from enemy 3 Answers
Instantiate on value change 2 Answers