#Help with adding way for health to go down

13 messages · Page 1 of 1 (latest)

stiff walrus
#

oh hold on give me a moment i might've figured it out

#

yea ok nvm i'll post

#
#

first is player movement (look at just the health and collider related stuff pls)
2nd is the boss trying to damage player script

#

i realised with the 3rd that i've coded in two scripts relating to health and i don't know why

#

if i haven't posted enough info lmk

glacial geode
terse cedar
#

Just speaking in general, usually for health I would have a IDamagable interface, and include that on anything that should have health and take damage, enemies, player, random barrels, breakable windows, etc, I would then use a serialized class that some mono behavior manages to store the health for taking damage and healing, a event can be fired for UI or other systems to respond to the health change, so interactions may look something like this:

public interface IDamagable
{
public void DoDamage(int value);
}

[System.Serializable]
public class HealthSystem
{
public int CurrentHP {get;set;}
public int MaxHP {get;set;} = 100;

public void Init() {CurrentHP = MaxHP;}

public void ChangeHP(int value)
{
//clamp checks...
CurrentHP += value; //would be a negative for damage taken, positive for healing
}
}

public void SomePlayer : Mono, IDamagable
{
public System.Action<int> onHPChange;

HealthSystem health = new HealthSystem();

void Start() {health.Init();}

void DoDamage(int value)
{
//logic for damage mitigation, etc...
health.ChangeHP(value);
onHPChange?.Invoke(health.CurrentHP);
}
}

public class SomeUI : Mono
{
public Slider hpBar;

void SomeInitFunc(SomePlayer player)
{
player.onHPChange += OnHP; //always remember to unsubscribe from things you subscribe to
}

void OnHP(int current)
{
hpBar.value = current; //could animate this value with a coroutine or Update if you wanted...
}
}

Not intended to be verbatim code, just showing what a general implementation could look like for context, with the interface you can put that on anything that should take damage, give that "anything" a HealthSystem to manage, and whatever systems need to care about it just need a reference to that "anything" (in your case, the player) and it can handle updating UI or playing animations or whatever in response to HP changing on that "anything", it also lets you separate your logic a bit, but I donno if that is your exact issue

stiff walrus
#

so i couldn't fully explain myself

#

@terse cedar sorry about that, thank you however i will try and look into this anyway

terse cedar
# stiff walrus <@168578802421727232> sorry about that, thank you however i will try and look in...

No worries, life happens and deadlines can sneak up on you - if you did want to do some learning outside of your program, interfaces and events are certainly very interesting and potentially helpful topics to look into and experiment with, paired with some architecture/programming patterns, I find it can help you approach many kinds of problems, and if programming is something you are interested in, I would take the time to ask your teachers some questions after classes as well to further your own understanding, GL!

stiff walrus