1
\$\begingroup\$

I'm attempting to create a simple mobile game (C#) that involves a simple three-heart life system. After searching for hours online, many of the solutions use OnGUI (which is apparently horrible for performance) and the rest are too complicated for me to understand and add to my code. The other solutions involve using a single texture and just hiding part of it when damage is taken. In my game, however, the player should be able to go over three hearts (for example, every 100 points).

Sebastian Lague's Zelda-Style Health is what I'm looking for, but even though it's a tutorial there is way too much going on that I don't need or can't customize to fit in mine.

What I have so far is a script called HealthScript.cs which contains a variable lives. I have another script, PlayerPhysics.cs which calls HealthScript and subtracts a life when an enemy is hit. The part I don't get is actually drawing the hearts.

I think I understand what needs to happen, I just am not experienced enough with Unity to know how.

  1. The Start function should draw three (or whatever lives is set to) hearts in the top right corner. Since the game should be resolution-independent to accommodate the various sizes of Android devices, I'd rather use scaling rather than PixelInset.
  2. When the player hits an enemy as detected by PlayerPhysics.cs, it should subtract from lives. I think that I have this working using this.GetComponent<HealthScript>().lives -= 1 but I'm not sure if it actually works. This should trigger a redraw of the hearts so that there are now two hearts.
  3. The same principle would apply for adding hearts when a score is reached, except when lives > maxHeartsPerRow, the new hearts should be drawn below the old ones.

I realise I don't have much code to show but believe me; I've tried for quite some time to figure this out and have little to show for it. Any help at all would be welcome; it seems like it shouldn't take that much code to put an image on the screen for each life there is, but I haven't found anything yet.

Thanks!

\$\endgroup\$
  • \$\begingroup\$ This should be much easier to accomplish using the new UI system that just launched with the 4.6 beta \$\endgroup\$ – ssb Aug 22 '14 at 2:34
  • \$\begingroup\$ Hmm. I'll have to look into that although I can't find any documentation available yet. \$\endgroup\$ – Matt Aug 22 '14 at 4:07
  • \$\begingroup\$ The unity YouTube channel has some examples up I believe \$\endgroup\$ – ssb Aug 22 '14 at 4:20
  • \$\begingroup\$ A cheat around way would be just having a array of objects which you enable/disable, at least until the new UI comes out of beta. \$\endgroup\$ – Dimitri mx Aug 22 '14 at 13:11
  • \$\begingroup\$ Cannot speak for Unity, but I implemented simple cooldown indicator(basically the same thing, just green and vertical) with few lines of shader code. \$\endgroup\$ – wondra Aug 22 '14 at 16:14
1
\$\begingroup\$

Yeah, a simple for loop will suffice for this.

for (int i = 0; i > Hearts; i++) {

  int col = i % 9;
  int row = Mathf.Floor(i / 9f);

  GUI.DrawTexture(new Rect(10 + (74*col), 10 + (74*row), 64, 64), heartTexture, ScaleMode.ScaleToFit);

}

Also, note the names / values may be changed, if your hearts are smaller, or you'd like less space in between hearts, I would recommend it.

\$\endgroup\$
0
\$\begingroup\$

You can use GUILayout which is from before the 4.6 beta to accomplish this. I would create a new area in the top right corner, then have a flexible space inbetween the left bound and the last heart. You can create a new row by having a vertical area. This is how I'd make the hierarchy

-BeginArea
| -BeginVertical
| | -BeginHorizontal
| | | -FlexibleSpace
| | | -Heart (if available)
| | | -Heart (if available)
| | | -Heart (if available)
| | -EndHorizontal
| | -BeginHorizontal
| | | -FlexibleSpace
| | | -Heart (if available)
| | | -Heart (if available)
| | | -Heart (if available)
| | -EndHorizontal
| | 
| | ...
| |
| -EndVertical
-EndArea

To create the first row of hearts, just loop draw one for every min(maxHeartsPerRow, hearts). You will then have (hearts - mod(hearts, maxHeartsPerRow)) / maxHeartsPerRow more rows. Then, until you are on the last row, fill each row with maxHeartsPerRow hearts. The last row you need to draw mod(hearts, maxHeartsPerRow) hearts.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.