Search Unity

Combat Text Display Problem

Discussion in 'Scripting' started by Eiznek, Aug 28, 2012.

  1. Eiznek

    Eiznek

    Joined:
    Jun 9, 2011
    Posts:
    374
    Getting a very odd result. I'm using a script that I previously wrote, to save time. I've been trying to figure out why its having the odd result.

    I'm using NGUI. I have two camera's set up. The 2D Camera that is Ortho and the main camera which is perspective, Its kind of 2.5d game however. Also the main camera has an offset that is very minor.


    Originally the
    Code (csharp):
    1. Vector3 v = worldCam.WorldToViewportPoint(myTarget.transform.localPosition);
    was just position, However it only displays if I use local position.

    The floating combat text object has the correct camera's assigned at run time.

    Code (csharp):
    1. public void DisplayText(string message, Color color, GameObject target, float displayTime, float textSizeMod, bool critHit, bool isNegative)
    2.     {
    3.         myTarget = target;
    4.         if(critHit)
    5.         {
    6.             SetColor(new Color(255,165,0, 1));
    7.             SetSize(1f);
    8.         }
    9.         else
    10.         {
    11.             SetColor(color);
    12.             SetSize(textSizeMod);
    13.            
    14.         }
    15.         SetLabel(message);
    16.         CameraFollow camFollow = worldCam.GetComponent(typeof(CameraFollow)) as CameraFollow;
    17.         Vector3 offset;
    18.         if(camFollow.useR1)
    19.             isNegative = false;
    20.         else
    21.             isNegative = true;
    22.         Vector3 v = worldCam.WorldToViewportPoint(myTarget.transform.localPosition);
    23.         v = guiCam.ViewportToWorldPoint(v);
    24.         v.z = 0;
    25.         myTransform.localPosition = v;
    26.         worldCam = NGUITools.FindCameraForLayer(myTarget.layer);
    27.        
    28.         StartCoroutine(DisplayDelay(displayTime, isNegative) );
    29.     }
    30.    
    31.     IEnumerator DisplayDelay(float time, bool negative)
    32.     {
    33.         display = true;
    34.         time = Time.time + time;
    35.         Vector3 v = worldCam.WorldToViewportPoint(myTarget.transform.localPosition);
    36.         v = guiCam.ViewportToWorldPoint(v);
    37.         v.z=0;
    38.         while(Time.time < time)
    39.         {
    40.             v.y+=0.1f;
    41.             if(negative)
    42.                 v.x-=0.1f;
    43.             else
    44.                 v.x+=0.1f;
    45.             myTransform.localPosition += v;
    46.             Debug.Log(v);
    47.             yield return new WaitForSeconds(0.02f);
    48.         }
    49.         DisableMe();
    50.     }
    **I was also doing Lerp before at this -> myTransform.localPosition += v; but it seemed to not work either.


    This is the piece of code that calls the Pool and sets up the info. I have Objects in the hierarchy of the Player/Enemy's where I want the 2D objects to display. So it uses those as the reference places.

    Code (csharp):
    1.  GameObject pivot = eScript.forwardDirection.FindChild("FloatingTextObject").gameObject;
    2.                             GameObject healthBarPivot = eScript.forwardDirection.FindChild("HealthBar").gameObject;
    3.                             GameObject temp = _PoolingManager.Instance.ActivatePooledItemWithCheck("OHB", healthBarPivot);
    4.                             if(temp != null)
    5.                             {
    6.                                 hb = temp.transform.GetChild(0).GetComponent("PopUpHealthBar") as PopUpHealthBar;
    7.                                 hb.DisplayHealthBar(healthBarPivot, 0.5f, infoScript);
    8.                             }
    9.                        
    10.                             GameObject ft = _PoolingManager.Instance.ActivatePooledItem("FT");
    11.                             FloatingText sct = ft.GetComponent(typeof(FloatingText)) as FloatingText;
    12.                             sct.DisplayText( ""+ infoScript.myVitals[i].Modify(-mod), Color.white, pivot, 0.5f, 0.8f, false, true);
    Here's a small video of whats happening. In my mind the text should move either left or right x wise and also in y direction.
    https://dl.dropbox.com/u/32175794/combatTextOddity/combatTextOddity.html
     
    Last edited: Aug 28, 2012