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

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #18196
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    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 the ExcessHealth 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 current excessHealth. Hence, instead of using Clamp(), we should be using Max() 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.

    #18197
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    1
    ::

    Your analysis of the issue with the ExcessHealth setter is correct, and your proposed solution using Mathf.Max() is a good way to fix the problem. However, you should ensure that the logic for updating the excessHealth 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:

    1. Use of Mathf.Max(): This ensures that excessHealth can increase indefinitely and will only clamp to 0 if a negative value is set.
    2. UI Update: The UI update call will reflect the new value of excessHealth whenever it changes.
    3. 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:

    1. Thread-Safety: If your game runs on multiple threads, you may need to consider making this setter thread-safe.
    2. 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.
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: