Simple heart container script for 2D game (Unity)?
Posted
by
N1ghtshade3
on Game Development
See other posts from Game Development
or by N1ghtshade3
Published on 2014-08-22T00:43:57Z
Indexed on
2014/08/22
4:28 UTC
Read the original article
Hit count: 392
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.
- 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 thanPixelInset
. - When the player hits an enemy as detected by
PlayerPhysics.cs
, it should subtract fromlives
. I think that I have this working usingthis.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. - 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!
© Game Development or respective owner