How do I keep health bar width & add to max health

0 favourites
  • 6 posts
From the Asset Store
Kids Game
$24.50 USD
50% off
New Sounds Added Update: 115 new sound effects added for no additional cost!
  • So using a health bar in a game and have it setup for the health bar set width to player instance health variable.

    the health value is set to the same width of the health bar width.

    What I am wanting to do in the game is allow the player to upgrade health to give them more health. How do I do this yet keep the health bar the same width?

    So the width is 110 and the health value is 110, if player does the upgrade and say adds 20 health the health value changes to 120 but I need the health bar width to stay at 110

  • You can use lerp function.

    Healthbar -> Set width to Lerp(0, 110, (player.currentHealth/player.maxHealth))

    So, for example if player.maxHealth=130 and currentHealth=65, then the healthbar.width will be set to 55px

  • You can use lerp function.

    Healthbar -> Set width to Lerp(0, 110, (player.currentHealth/player.maxHealth))

    So, for example if player.maxHealth=130 and currentHealth=65, then the healthbar.width will be set to 55px

    dop2000 Ok cool thank you. I am not familiar with Lerp so I will look that up in the manual and do a little reading on that. Appreciate the help

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Ok so the section on the lerp kinda made sense to me, that makes things really easy lol.

    One thing though the health bar width at start of game is just a little bit shorter than the width of the 110, or at least what it shows at 110 in the editor. I double checked the width of the health bar and it is 110

  • Check that you player's health and fullHealth are the same at the start of the game.

    Actually, you don't even need lerp (I like it so much I use it everywhere ), this formula is easier:

    bar.width=110 * (player.health/player.fullHealth)

  • Check that you player's health and fullHealth are the same at the start of the game.

    Actually, you don't even need lerp (I like it so much I use it everywhere ), this formula is easier:

    bar.width=110 * (player.health/player.fullHealth)

    dop2000 yep that did it, the health was initially set to the 110 but for some reason it got reset to 0. Put it back to 110 and it worked like a charm.

    haha I like the lerp so I think I'll keep it in there and get used to using it. Thank you so much for the help, you've been awesome

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)