playMaker

Author Topic: Hearts - NOT health bar [SOLVED]  (Read 1781 times)

Haserhud

  • Playmaker Newbie
  • *
  • Posts: 19
Hearts - NOT health bar [SOLVED]
« on: February 13, 2020, 09:53:58 AM »
Hey everyone!

I'm trying to figure out enemy damage to the player. My vision is to have a simple heart system. Three hearts on the screen. Each time the player is touched by an enemy or harmful object, one heart is subtracted. At zero hearts, it's back to the start of the level. Super simple stuff.

Unfortunately, the only tutorials (and most of the literature) online talks about a health bar system, which seems completely different from what I'm trying to do.

Any suggestions on how to go about this/resources I can use?

Thanks!
« Last Edit: February 16, 2020, 07:54:30 AM by djaydino »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Hearts - NOT health bar
« Reply #1 on: February 13, 2020, 02:52:18 PM »
Hi.
You can use a int for your health and set to 3.

then use a int switch with 0,1,2,3

on hit deduct 1 from the health, then send event to check (using the int switch)
after the switch you can use set image to set each health image.

so if 0 set al transparent or swap with empty image, if 1 set 1 heart image, etc

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: Hearts - NOT health bar
« Reply #2 on: February 13, 2020, 06:40:10 PM »
First make the health system without regards to how it looks like.

1) Player and harmful objects need colliders. Decide if you go with 2D or 3D physics. 2D is definitely easier, but requires a specific setup and not all Unity features are fully available, most noticeable the lack of navmesh ( thought there are alternatives). Very important: this determines the components and the action you’ll use. All the 2D physics elements and actions have 2D in the name. It will not work when you mix.

2) Next, you decide on triggers or colliders. It’s both the same collider component in different shapes. You switch this to trigger by checkbox in the collider options. You then need to use specific trigger actions instead of collision actions. In a nutshell, collisions use forces that push the objects around. If you don’t want that or don’t use that, set them to triggers (it seems to be that triggers want a rigidbody on the other object to get triggered).

3) It’s most versatile to put the damage system on its own game object and as a child of the holder, but if you want to keep it simple, you can just use the holder object (i.e. the player). It must be on the same object that has the collider/trigger (at least in a simple version).

4) The harmful object likewise need a collider/trigger. Again, determine if you want collision or just detect if something touches the collider/trigger object.

5) Set the layers for the objects with colliders. It’s a bit confusing at first, but of course will start to make sense with practice. Open project settings and under your physics, normal or 2D, find the collision matrix. By default everything collides with most stuff. But here you need to set a checkmark where the two layers of damage-dealer and receiver meet.

6) Your system seems super easy, so you can theoretically make the player damage itself on collision. To do this, and say you went with triggers 2D, you add the actions on “trigger enter 2D” into the fsm on the player object where the trigger sits. This basically waits until it collides with something. If so, int substract (or int add -1) from hearts int variable of three. After that, make a state that int compares against 0, and if zero, restart level action. Super cool trick: instead of the on trigger enter 2D action, you can use the system global event of that name. This leads to a separate state, and runs from there (and not from start).


7) For the visual UI: if it is really simple, you can use Djaydino’s idea with the int switches. I would simply make one heart UI image, duplicate and arrange on the canvas, and in each int switch states, use the activate game object action to show / hide the correct amount. If the player is a prefab, each UI heart needs its own tag, so you can find it with get child or find child (the one to look for tags).

I probably forgot a thing or two, but it should cover most of it.
« Last Edit: February 14, 2020, 05:49:08 PM by Thore »

Haserhud

  • Playmaker Newbie
  • *
  • Posts: 19
Re: Hearts - NOT health bar
« Reply #3 on: February 14, 2020, 04:57:48 PM »
This is wonderful, guys. THANK YOU.

I was able to get everything working aside from the Set Image Sprite. During play, the heart images change in the inspector upon collision, but the image is static in the actual game space itself. Any idea what I did incorrectly?
The object is UI with Sprite Renderer and Playmaker FSM components on it.

I did this before I saw your suggestion about activating game objects, Thor. I may try that if this doesn't work although it's so close to working.
« Last Edit: February 14, 2020, 04:59:52 PM by Haserhud »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Hearts - NOT health bar
« Reply #4 on: February 15, 2020, 05:48:07 AM »
Hi, if you want the ui above the head of player or enemy, set the canvas Render mode to 'world space'

Then set the canvas as child on the player/enemy or have it follow the player/enemy
You probably will need to adjust the size and position

if you want it on a certaint place on the screen (for example top right)

Then you can use the Render mode : screen space overlay OR screen space camera.

Then scaler you can for example set to 'Scale with Screen Size'

But you have to try out and look what fits best for you :)

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: Hearts - NOT health bar
« Reply #5 on: February 15, 2020, 06:59:14 AM »
Hi, if you want the ui above the head of player or enemy, set the canvas Render mode to 'world space'

Then set the canvas as child on the player/enemy or have it follow the player/enemy
You probably will need to adjust the size and position

if you want it on a certaint place on the screen (for example top right)

Then you can use the Render mode : screen space overlay OR screen space camera.

Then scaler you can for example set to 'Scale with Screen Size'

But you have to try out and look what fits best for you :)

:)
https://youtu.be/BLfNP4Sc_iA?t=924
« Last Edit: February 15, 2020, 07:01:01 AM by Thore »

Haserhud

  • Playmaker Newbie
  • *
  • Posts: 19
Re: Hearts - NOT health bar
« Reply #6 on: February 15, 2020, 04:34:40 PM »
I got it working.

You all rock!

 :D :D :D