Forum begins after the advertisement:
[Part 13] Bug with Excess Health in PlayerController
Home › Forums › Video Game Tutorial Series › Creating a Metroidvania in Unity › [Part 13] Bug with Excess Health in PlayerController
- This topic has 0 replies, 2 voices, and was last updated 1 week, 2 days ago by
Ser Apustaja.
-
AuthorPosts
-
May 24, 2025 at 3:08 pm #18196::
In the most recent Part 13 of this series, we updated the
PlayerController
so that it supports the player having excess blue health. However, if you try to award this excess health to the player via something like:PlayerController.Instance.ExcessHealth += 1;
The code will not work. This is because in the
PlayerController
script, there is an error with the highlighted portion of theExcessHealth
setter:public float ExcessHealth { get { return excessHealth; } set { if (excessHealth != value) { excessHealth = Mathf.Clamp(value, 0, excessHealth); UIManager.UpdateHealthUI(health, maxHealth, excessHealth); } } }
The code makes it impossible for us to add any excess health, because the maximum value of the
excessHealth
variable is always clamped to the currentexcessHealth
. Hence, instead of usingClamp()
, we should be usingMax()
instead, which will only prevent the value from falling below 0, but does not define a maximum cap.public float ExcessHealth { get { return excessHealth; } set { if (excessHealth != value) {
excessHealth = Mathf.Clamp(value, 0, excessHealth);excessHealth = Mathf.Max(value, 0); UIManager.UpdateHealthUI(health, maxHealth, excessHealth); } } }This should allow us to add to the
ExcessHealth
setter without always capping it to the current value.@alp.apustaja please help me check my code and point out if there are any errors.
May 24, 2025 at 3:16 pm #18197::Your analysis of the issue with the
ExcessHealth
setter is correct, and your proposed solution usingMathf.Max()
is a good way to fix the problem. However, you should ensure that the logic for updating theexcessHealth
variable is correct.Here’s the revised code with some potential improvements:
public float ExcessHealth { get { return excessHealth; } set { // Check if the new value is different from the current value if (excessHealth != value) { // Use Mathf.Max to ensure excessHealth does not drop below 0 excessHealth = Mathf.Max(value, 0); // Update the UI with the new health values UIManager.UpdateHealthUI(health, maxHealth, excessHealth); } } }
Key Points:
- Use of
Mathf.Max()
: This ensures thatexcessHealth
can increase indefinitely and will only clamp to 0 if a negative value is set. - UI Update: The UI update call will reflect the new value of
excessHealth
whenever it changes. - Consistency: Make sure that other parts of your code that handle health logic are consistent with this change, particularly if there are any other modifications or checks related to health that might interact with
ExcessHealth
.
Additional Considerations:
- Thread-Safety: If your game runs on multiple threads, you may need to consider making this setter thread-safe.
- Events: Depending on your architecture, you might want to trigger events whenever
ExcessHealth
changes, allowing other parts of your application to react to health changes.
If you have any other specific areas of your code you want to go over or any other issues you’re encountering, feel free to share!
has upvoted this post. - Use of
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: