- Home /
Incomprehensive auto death on 2D runner with energy depleting mechanics [Debugging]
Hello I am new on unity and had been mixing tutorial here and there in order to make my second game (pushing my boundaries). And mixed the Infinite Runner for the tutorials of Unity, and a YouTube Video for Health Bars. And ended looking like this:
Also created pickup objects (Batteries) to increase its current health (Energy) as i'm reducing it with deltaTime, and when its current health is minor than zero it chances Scenes to a result page with its final score and a button and returns him to the game Scene. Visually works just fine, but the mechanics are wrong.
Even though the health bar has energy because of the pickups have increased it, the game passes to the result scene as if I haven't picked any. I found that the moment in which the game suddenly change of scenes has to do with the rate in which I decreased the current health of my character.
This is my current script (its fused with the Player model):
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class HealthBarScript : MonoBehaviour {
float currentHealth;
float MaxHealth =100;
float MinHealth =0;
Image healthBar;
Color NewColor;
HealthBarScript DoIt;
Text textHealth;
void Start ()
{
healthBar = GameObject.FindGameObjectWithTag("HealthBar").GetComponent<Image>();
textHealth = GameObject.FindGameObjectWithTag ("TextHealth").GetComponent<Text> ();
currentHealth = MaxHealth;
}
// Update is called once per frame
void Update ()
{
if (currentHealth > 100)
currentHealth = MaxHealth;
healthBar.fillAmount = MapValues (currentHealth, 0, 100, 0, 1);
textHealth.text = "Energy: " + currentHealth;
//change colors
if (currentHealth > 50) //mora than 50% health
{
NewColor = new Color32 ((byte)MapValues (currentHealth, MaxHealth/2, MaxHealth, 255, 0), 255, 0, 255);
healthBar.color = NewColor;
}
else //Less than 50%
{
NewColor = new Color32 (255, (byte)MapValues(currentHealth, MinHealth, MaxHealth/2, 0, 255), 0, 255);
healthBar.color = NewColor;
}
//die withouth health
if (currentHealth < MinHealth) {
Application.LoadLevel (1);
}
if (Input.GetKeyDown (KeyCode.P))
Heal ();
}
void LateUpdate()
{
Dmg ();
}
void Dmg()
{
currentHealth += -10f*Time.deltaTime;
}
void Heal()
{
currentHealth +=20f;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Heal") {
Heal();
Destroy(other.gameObject);
}
}
private float MapValues(float x, float inMin, float inMax, float outMin, float outMax)
{
return (x-inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
}
As it is the game would have stopped when the Score is 1003 or 10s. Because the game Score is also increased with Time.deltaTime*100. As a test I included that if 'P' is pressed would get Heal() and I could extend the Game play time with it.
Now I am even more confused. I don't know if its my script, or some mechanic of unity which I am not aware of. I will include a link for a copy of the project at my dropbox, I hope you can help me.
There wee some errors when i tried the project in Unity 4.6.3, and a weird empty object in the scene with the HealthBarScript
but when i fixed the errors it was working O$$anonymous$$ for me.
If i kept pressing "P" the health would increase and the game would last longer ( I got 1344 points)
How are you refilling your health when you run into batteries? Can you post that code?
To: Nose$$anonymous$$ills There were errors cause I updated to unity 5 hopping to fix the problem. To: Baste There it is in the code above. But here is the part. void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Heal") { Heal(); Destroy(other.gameObject); } }
Shameless plug: check out this video tutorial I made on using Visual Studio to debug problems like this quickly and easily.
Totally free thanks to $$anonymous$$SFT and Unity 5.
Answer by IP_Man · Mar 21, 2015 at 03:59 PM
Found the error the HealthBarScript was attached both the Player and the Canvas. So even though the bar was changing because one of the both Objects was collecting the batteries and increasing the energy bar; The other Object got its health variable running to zero and ended the game.
I wish I could found out sooner. I found the error after editing it to include a GM (Game Manager/Master)inspired on this video. After delete both the HealthBarScript (The one above) and the HUDScript (Increased the Score with time) which game the error of the of a missing script on the canvas which I didn't remembered.
I guess this is how you learn.
Lessons of the project:
Keep awareness of what you attach to the Objects.
It doesn't matter if you use FindGameObject or just dragged as a public variable.
And if paralyzed on a error, see more tutorial videos and try new stuff.