Damage Popup Above Enemy Head instead of at Ground?

Hi guys,

I got the attached script below, it works well, except the damage popups at enemy feet at ground level and then it goes up using animation. It works fine with player because it uses popup marker and i can change its location.

However, I can’t figure out how to make it work without popup marker for enemy.

Basically I want the damage to popup above enemy head say at y=1.8.

Someone who is experienced in coding, will figure it out pretty easily.

Any help would be appreciated.

Thanks

using UnityEngine;

public class DamagePopupManager : MonoBehaviour
{
    [SerializeField] private GameObject m_damagePopupPrefab;
    [SerializeField] private float m_positionVariation;
    [SerializeField] private bool m_dontDestroyOnLoad;

    private GenericPool<DamagePopup> m_popupPool;
    private static DamagePopupManager m_instance;

    private void Awake()
    {
        m_instance = this;
        m_popupPool = new GenericPool<DamagePopup>(CreateNewPopup, FreePopup, null);

        if(m_dontDestroyOnLoad)
            GameObject.DontDestroyOnLoad(gameObject);
    }

    private void OnDestroy()
    {
        if(m_instance == this)
        {
            m_instance = null;
        }
    }

    private void ShowInternal(Vector3 position, int damage, int style)
    {
        DamagePopup popup = m_popupPool.GetObject();
        popup.gameObject.SetActive(true);
        popup.transform.position = position + popup.transform.right * UnityEngine.Random.Range(-m_positionVariation, m_positionVariation);
        popup.Show(damage, style);
    }

    private DamagePopup CreateNewPopup()
    {
        GameObject gameObject = GameObject.Instantiate<GameObject>(m_damagePopupPrefab);
        gameObject.transform.SetParent(transform, false);

        DamagePopup popup = gameObject.GetComponent<DamagePopup>();
        popup.Done += HandlePopupDone;

        return popup;
    }

    private void FreePopup(DamagePopup popup)
    {
        popup.gameObject.SetActive(false);
    }

    private void HandlePopupDone(DamagePopup popup)
    {
        m_popupPool.FreeObject(popup);
    }

    public static void Show(Vector3 position, int damage, int style)
    {
        m_instance.ShowInternal(position, damage, style);
    }

    public static void Show(Transform position, int damage, int style)
    {
        Transform marker = position.Find("popup_marker");
        if(marker != null)
            m_instance.ShowInternal(marker.position, damage, style);
        else
            m_instance.ShowInternal(position.position, damage, style);
    }

    public static void ShowStandard(Transform position, int damage, bool isCritical)
    {
        Transform marker = position.Find("popup_marker");
        Vector3 pos = marker != null ? marker.position : position.position;

        if(isCritical)
            m_instance.ShowInternal(pos, damage, DamagePopup.STYLE_DAMAGE | DamagePopup.STYLE_CRITICAL);
        else
            m_instance.ShowInternal(pos, damage, DamagePopup.STYLE_DAMAGE);
    }
}

When creating a new popup you don’t set the position so the position will be the parents position. Adding this probably will fix it:

popup.transform.position += new Vector3(0, 1.8f, 0)

Does this work?

1 Like

I have’t tried it yet. I will do it when i get home in couple of hrs.

Btw just to clarify where should I exactly add this line?

Under private DamagePopup CreateNewPopup() ?

Thanks for your help

Add it after gameobject.transform.setParent in the createnewpopup methoud.

When i added the line as shown below :

    private DamagePopup CreateNewPopup()
    {
        GameObject gameObject = GameObject.Instantiate<GameObject>(m_damagePopupPrefab);
        gameObject.transform.SetParent(transform, false);
        popup.transform.position += new Vector3(0, 1.8f, 0)
           
        DamagePopup popup = gameObject.GetComponent<DamagePopup>();
        popup.Done += HandlePopupDone;

        return popup;
    }

I get the following error in unity and game won’t start:

Assets/_Core/Scripts/Gameplay/DamagePopupManager.cs(43,27): error CS1525: Unexpected symbol `DamagePopup’

The DamagePopup changes color when i add the line.

It’s because you are trying to access a component that hasn’t been created yet. Change the position of the gameObject’s transform instead, as the gameObject has been created: gameobject.transform.position

It’s not working, it’s still spawning at ground level. :frowning:

Hmm, that should set the correct position for the new popup. Are you calling any other methoud with the popup as that can change the value?

No that’s only method I am using.
Maybe i am not adding the line correctly.
How would you write it?

I would do it I’ve this:
gameObject.transform.position += new Vector3(0, 1.8f, 0)
Is this what you did?
Also can you show the DamagePopup class?

Ya this is what I did. Strange, it didn’t move the damage popup to 1.8 position.

Can you show the DamagePopup class?

Here it is:

using UnityEngine;

public class DamagePopup : MonoBehaviour
{
    public const int STYLE_DAMAGE = 2;
    public const int STYLE_PLAYER = 4;
    public const int STYLE_POISON = 8;
    public const int STYLE_LIFE_STEAL = 16;
    public const int STYLE_CRITICAL = 32;

    public event System.Action<DamagePopup> Done;

    [SerializeField] private TextMesh m_textMesh;
    [SerializeField] private Animator m_animator;
    [SerializeField] private Color m_damageColor = Color.white;
    [SerializeField] private Color m_playerDamageColor = Color.red;
    [SerializeField] private Color m_poisonColor = Color.yellow;
    [SerializeField] private Color m_playerPoisonColor = Color.magenta;
    [SerializeField] private Color m_lifeStealColor = Color.green;

    public void Show(int damage, int style)
    {
        m_textMesh.text = damage.ToString();

        if((style & STYLE_LIFE_STEAL) != 0)
        {
            m_textMesh.color = m_lifeStealColor;
        }
        else if((style & STYLE_POISON) != 0)
        {
            if((style & STYLE_PLAYER) != 0)
                m_textMesh.color = m_playerPoisonColor;
            else
                m_textMesh.color = m_poisonColor;
        }
        else
        {
            if((style & STYLE_PLAYER) != 0)
                m_textMesh.color = m_playerDamageColor;
            else
                m_textMesh.color = m_damageColor;
        }

        if((style & STYLE_CRITICAL) != 0)
            m_animator.SetTrigger("ShowCritical");
        else
            m_animator.SetTrigger("Show");
    }

    private void OnDamagePopupDone()
    {
        if(Done != null)
            Done(this);
    }
}

Super late to this, but just in case somebody stumbles on this via google like I did, that initial error is caused by the missing semicolon at the end of: popup.transform.position += new Vector3(0, 1.8f, 0) That’s why it says “DamagePopup” in the next line is an “Unexpected symbol.”