Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by IP_Man · Mar 15, 2015 at 07:14 PM · 2dbugbeginnerrunner

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.

example.png (12.1 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · Mar 20, 2015 at 01:22 PM 0
Share

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)

avatar image Baste · Mar 20, 2015 at 01:32 PM 0
Share

How are you refilling your health when you run into batteries? Can you post that code?

avatar image IP_Man · Mar 20, 2015 at 06:03 PM 0
Share

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); } }

avatar image tigertrussell · Mar 20, 2015 at 06:19 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Boxcollider2d sinks into Ground when falling on a non straight surface 0 Answers

Object Changing Y Coordinate in Game View 0 Answers

How slow motion everything except animation 1 Answer

What is wrong with my Counter Script? (C#) 1 Answer

Why is my 2D Sprite squashed during Play? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges