Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Damage text popup position issue

Discussion in 'Scripting' started by patrick750, Mar 26, 2020.

  1. patrick750

    patrick750

    Joined:
    Nov 29, 2019
    Posts:
    12
    Hi,

    I'm trying to build a damage popup system similar to what you might see in WoW.
    It swaps between moving up-left and up-right before disappearing.

    The issue I have is that its position in space is based on where the target is facing. Ideally I want to see the text popup above and to the left/right based on where I am standing rather than where they are facing. Here is an image of the issue:


    As you can see, the text appears to the left and right of where the target is facing despite that I am not looking directly at it.

    Here is the code that is run first when damage text is called.

    Code (CSharp):
    1.     public DamagePopup Create(Vector3 position, float damageAmount)
    2.     {
    3.         Transform damagePopupTransform = Instantiate(damagePopup, position, Quaternion.identity);
    4.         DamagePopup damagePopupScript = damagePopupTransform.GetComponent<DamagePopup>();
    5.         damagePopupScript.Setup(damageAmount);
    6.         return damagePopupScript;
    7.     }
    Here is the important code from the Setup method that is run based on that.

    Code (CSharp):
    1.     public void Setup(float damageAmount)
    2.     {
    3.         textMesh.SetText(damageAmount.ToString());
    4.         textMesh.transform.LookAt(Camera.main.transform.position);
    5.         textMesh.transform.Rotate(0, 180, 0);
    6.  
    7.         int textMoveVector = Random.Range(1, 3);
    8.         if (textMoveVector == 1)
    9.         {
    10.             moveVector = new Vector3(20, 10);
    11.         }
    12.         else if (textMoveVector == 2)
    13.         {
    14.             moveVector = new Vector3(-20, 10);
    15.         }
    This is also being run in Update() and might be important to the issue.

    Code (CSharp):
    1. transform.position += moveVector * Time.deltaTime;
    2.         moveVector -= moveVector * 10f * Time.deltaTime;
    I've managed to get the text to face me regardless of where i am (with the Camera.LookAt code) but am stuck figuring out how to dynamically make the text appear at different vector positions based on where I am.

    Any point in the right direction would be greatly appreciated.
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    You have to define the moveVector3 based on where you are. Right now it's always the same. You could do so for example by using
    moveVector3 = Camera.main.transform.right * 20f + Vector3.up * 10f;
     
  3. patrick750

    patrick750

    Joined:
    Nov 29, 2019
    Posts:
    12
    I narrowed it down to that but honestly did not figure the answer was that simple. Thought Camera.main.transform would would place the text at my actual camera's location. Appreciate this reply so much :)