- Home /
My health decreases to fast.
I want it where if I stand in the trigger than my health will go down slowly, but I don't know how to make it go slowly. I used a int value to make it a little slower which didn't really slow it down much, so I tried a float value, but it says that I cannot use a float value. Somebody help. Heres my code. It's line 52 where I'm having the problem.
using UnityEngine;
using System.Collections;
public class Soldier : MonoBehaviour {
public int health = 100;
public int hitSpeed = 1;
public GUIText healthText;
public GUIText dead;
public bool canPickUpHealth;
public bool isHit = false;
public bool isDead = false;
// Use this for initialization
void Start () {
SetHealthText();
dead.text = "";
canPickUpHealth = false;
isDead = false;
}
// Update is called once per frame
void Update () {
if(health >= 100)
{
health = 100;
SetHealthText();
}
if(health < 0)
{
health = 0;
SetHealthText();
}
if(health <= 0)
{
Time.timeScale = 0;
dead.text = "YOU ARE DEAD";
SetHealthText();
}
if(health == 100)
{
canPickUpHealth = false;
}
else
{
canPickUpHealth = true;
}
if(isHit)
{
health -= 15 * hitSpeed;
SetHealthText();
}
else
{
isHit = false;
}
}
void OnTriggerEnter(Collider other)
{
if(canPickUpHealth)
{
if(other.gameObject.tag == "Health" && health <= 100)
{
other.gameObject.SetActive(false);
health = health + 15;
SetHealthText();
}
}
if(other.gameObject.tag == "Enemy" && health > 0)
{
isHit = true;
}
}
void OnTriggerStay(Collider other)
{
if(other.gameObject.tag == "Enemy")
{
isHit = false;
}
}
void OnTriggerExit(Collider other)
{
if(other.gameObject.tag == "Enemy")
{
isHit = false;
}
}
void SetHealthText()
{
healthText.text = "Health: " + health.ToString();
if(health <= 0)
{
dead.text = "YOU ARE DEAD";
}
}
}
Answer by instruct9r · Dec 08, 2013 at 11:55 PM
first, change the declaration of health and hitSpeed to float. It tells you, that you can't use float, because you can't multiply int with float.
public float health = 100.0f;
public float hitSpeed = 1.0f;
then change the line where the health shoud go down
health -= Time.deltaTime * hitSpeed;
And then you adjust the speed with the hitSpeed attribute...
doesn't work. /Scripts/Soldier.cs(52,25): error CS0266: Cannot implicitly convert type float' to
int'. An explicit conversion exists (are you missing a cast?)
This is good but is there any possible way to use a int value because when I get hurt, my health goes to something like 97.4567 ins$$anonymous$$d of just 97.
ok. You can convert the result number to a whole number with the $$anonymous$$athf functions. For example, the $$anonymous$$athf.Round. This will round the number to the nearest whole number. 10.1 = 10, 10.6 = 11, etc, etc.
So it shoud be something like this:
if(isHit)
{
health -= Time.deltaTime * hitSpeed;
health = $$anonymous$$athf.Round(health);
SetHealthText();
}
Or you can create one new int variable and assing it with the rounded number. Note, that you'll have to change the health with healthInt in the SetHealthText function, to display the proper number...
public int healthInt;
if(isHit)
{
health -= Time.deltaTime * hitSpeed;
healthInt = $$anonymous$$athf.RoundToInt(health);
SetHealthText();
}
If you don't want to round the number, you can use some of the other $$anonymous$$athf functions. http://docs.unity3d.com/Documentation/ScriptReference/30_search.html?q=$$anonymous$$athf
Check the Ceil, CeilToInt, Floor, FloorToInt.
One other way might work, if you include F0 in the brackets right after the ToString(). F0 will display only the whole number (if i remember right). F1 will display one number after the decimal and so on. So try just replacing the piece of code below, without using the $$anonymous$$athf function.
void SetHealthText()
{
healthText.text = "Health: " + health.ToString("F0");
if(health <= 0)
{
dead.text = "YOU ARE DEAD";
}
}
Anyway, this way i'm not sure how the number will be rounded. To the nearest or to the ceil / floor
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
OnTriggerStay 2 Answers
How to trigger a Script from an Object. 0 Answers