Search Unity

Set child position of gameobject to top of parent gameobject (health bar)

Discussion in 'Scripting' started by Gogin, Sep 21, 2018.

  1. Gogin

    Gogin

    Joined:
    Nov 9, 2013
    Posts:
    60
    Hi guys,

    I guess its really simple but I cannot get why its not working properly. I was trying to set gameobject child position to top of parent gameobject, please see screenshot below (heal bar):

    code:

    structureText = child
    this.gameobject = parent

    Code (CSharp):
    1.         structureText.transform.localPosition = new Vector3(0, this.transform.localScale.y, 0); // - structureText.transform.localScale.y - this.GetComponent<Renderer>().bounds.size.y
    2.  
     
  2. wtrebella

    wtrebella

    Joined:
    Mar 18, 2013
    Posts:
    35
    You're setting the y value from the localScale. Why?
     
  3. Gogin

    Gogin

    Joined:
    Nov 9, 2013
    Posts:
    60
    It's just sample code. I need move object up to tower which can have different scale. So its needs to be calculated with.
     
  4. Gogin

    Gogin

    Joined:
    Nov 9, 2013
    Posts:
    60
    [RESOLVED]

    Code (CSharp):
    1.     private void SetTextProperties()
    2.     {
    3.         structureText = new GameObject();
    4.         structureText.name = "Structure Text";
    5.  
    6.         Canvas canvas = structureText.AddComponent<Canvas>();
    7.         canvas.renderMode = RenderMode.WorldSpace;
    8.  
    9.         CanvasScaler cs = structureText.AddComponent<CanvasScaler>();
    10.         cs.scaleFactor = 1f;
    11.         cs.dynamicPixelsPerUnit = 60f;
    12.  
    13.         //How big is canvas! (10x10)
    14.         RectTransform objectRectTransform = structureText.GetComponent<RectTransform>();
    15.         objectRectTransform.sizeDelta = new Vector2(12, 6); //Edit plane for
    16.  
    17.         objectRectTransform.localScale = new Vector3(0.25f, 0.25f, 0.25f); //Smaller value, smaller text
    18.  
    19.         Font Arial = (Font)Resources.Load("Fonts/zrni_ rg"); //(Font)Resources.Load("Font/zrni_ rg.ttf"); //(Font)Resources.GetBuiltinResource(typeof(Font), "Arial.ttf");
    20.  
    21.         t = structureText.AddComponent<Text>();
    22.  
    23.         t.font = Arial;
    24.         t.fontSize = 1;
    25.         t.color = Color.black;
    26.  
    27.         t.alignment = TextAnchor.LowerCenter;
    28.  
    29.         float rectHeight = objectRectTransform.sizeDelta.y * objectRectTransform.lossyScale.y;
    30.         AlignTextUp(rectHeight, 1.7f);
    31.        
    32.         //Parent with mob object
    33.         structureText.gameObject.transform.SetParent(this.transform);
    34.  
    35.     }
    36.  
    37.     //Get proper height (y) of mob, less divisor => more up wards
    38.     private void AlignTextUp(float rectHeight, float divisor)
    39.     {
    40.    [B]     structureText.transform.localPosition = new Vector3(this.transform.position.x, this.transform.localScale.y + (rectHeight / divisor), this.transform.position.z); // - structureText.transform.localScale.y - this.GetComponent<Renderer>().bounds.size.y[/B]
    41.     }
     
unityunity