← Back to all addons

Health Bar shake when hurt

Posted by VIIЯLΞSS on May 8, 2024 at 4:05 PM

Feature1.3 VerifiedOfficially Added
💡 5

[Improved by Gyvr]

Usage: Add this code into the UiStatBar.cs

private void OnEnable()
{
    if (m_stat == EStat.Health)
    {
        GameManager.NotificationSystem.damageApplied.AddListener(OnDamageApplied);
    }
}

private void OnDisable()
{
    if (m_stat == EStat.Health)
    {
        GameManager.NotificationSystem.damageApplied.RemoveListener(OnDamageApplied);
    }
}

private void OnDamageApplied(CharacterBase target, DamageInputDescriptor damageInputDescriptor)
{
    if (target is Hero)
    {
        Shake();
    }
}

private void Shake()
{
    // Simple shake animation
    float shakeAmount = 5f; // adjust to your liking
    float shakeDuration = 0.2f; // adjust to your liking

    StartCoroutine(ShakeCoroutine(shakeAmount, shakeDuration));
}

private System.Collections.IEnumerator ShakeCoroutine(float shakeAmount, float shakeDuration)
{
    float elapsedTime = 0f;
    Vector3 initialPosition = m_slider.transform.localPosition;

    while (elapsedTime < shakeDuration)
    {
        float xOffset = Mathf.Sin(elapsedTime * 30f) * shakeAmount; // Increased multiplier from 20f to 30f
        float yOffset = Mathf.Sin(elapsedTime * 25f) * shakeAmount; // Increased multiplier from 15f to 25f
        m_slider.transform.localPosition = new Vector3(initialPosition.x + xOffset, initialPosition.y + yOffset, initialPosition.z);
        elapsedTime += Time.deltaTime * 2f; // Increased the time multiplier from 1 to 2
        yield return null;
    }

    m_slider.transform.localPosition = initialPosition;
}

Health Bar shake when hurt

💬 Comments (0)

Be the first to comment! Join our Discord to share your thoughts.

Want to continue the conversation?