0

I am trying to create a dimension overlay canvas in Unity. Managed to make one but having difficulties while rotating the gameObject(Cube).The Dimension display text are rotating but they are not repositioning themselves with the canvas and gameobject. Instead they rotate at around their own position. How can I make them rotate along with the Canvas so that they reposition themselves? Please see the screenshots and the code attached to the gameObject(Cube) below

Many Thanks in advance :)

Position of the dimension text and the worldspace canvas

Dimension texts are not changing position while rotating the game

Please see my code below

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class dimensionScript : MonoBehaviour
{

    //World space Canvas to attach on the GameObject
    public Canvas canvas;

    //Text to display the Length and Width of the Game object
    public Text lt, wt;

    public float l, w;
    public Vector3 angles,pos,sc;



    void Update()
    {

        angles = transform.eulerAngles;
        pos =transform.position;
        sc = transform.localScale;

        Vector3 pos2 = canvas.transform.position;
      

        // Length and Width of the GameObject
        l = transform.localScale.x;
        w = transform.localScale.z;

        // Positioning and scaling the canvas according to the scale of the gameobject
        canvas.transform.position = transform.position;        
        canvas.GetComponent<RectTransform>().sizeDelta = new Vector2(l+.5f,w + .5f);
        canvas.transform.eulerAngles = new Vector3(90f,angles.y,  0);

        float rw = canvas.GetComponent<RectTransform>().rect.width;
        float rh = canvas.GetComponent<RectTransform>().rect.height;
      

 lt.transform.position = new Vector3(pos2.x, pos2.y, pos2.z - rh / 2);
  wt.transform.position = new Vector3(pos2.x - rw / 2, pos2.y, pos2.z);

       // lt.transform.eulerAngles = new Vector3(90f, angles.y, 0);
       // wt.transform.eulerAngles = new Vector3(90f, angles.y, 0);


        lt.text = l.ToString();
        wt.text = w.ToString();

    }

}

enter image description here enter image description here

enter image description here

yousufali.n
  • 21
  • 2
  • 10
  • Well, you can place your Canvas under cube in hierarchy and let Unity handle all the rotations and positions for you. But I don't know if it is an option for your case. – obywan Dec 30 '20 at 12:11
  • Could you show how your Canvas structure and Inspector settings of your text objects look like? If set up correctly you wouldn't have to set any of their positions "manually" but just anchor them at the correct side of the canvas and scale it -> the UI does everything for you – derHugo Dec 30 '20 at 12:18
  • In particular your issue here is the `lt.transform.position` and accordingly `wt` .. you are setting absolute world space positions and don't take any rotation into account. As a quick and dirty fix you could probably do something like `lt.transform.position = lt.transform.rotation * new Vector3(pos2.x, pos2.y, pos2.z - rh / 2);` .. but in general you should rather use `localPosition` or as said .. configure the UI correctly ;) – derHugo Dec 30 '20 at 12:21
  • Thank you for the reply guys!. I just updated the question with Hierarchy and Inspector details. Please have a look. I also tried both the suggested methods, no luck so far.. – yousufali.n Dec 30 '20 at 13:21
  • I'm pretty sure you misunderstood me, because my method should work. I'll post it as an answer to include pictures. Wait a few minutes – obywan Dec 30 '20 at 13:47

2 Answers2

0

Ok, so what you need to do is to place Canvas root object under cube in scene hierarchy (as a child) like this:

enter image description here

After that just get rid of all code that change position and rotation of the canvas or text and you should be fine. In my example Canvas is placed at the bottom of the cube (-0.5 "y" coordinate) enter image description here

after that you can rotate and move your cube as you want and canvas will stick to it like any other gameobject placed as a child of that cube enter image description here

obywan
  • 854
  • 8
  • 14
  • Thank you for the post and explanation!. The problem I encountered in this method was during the gameobject scaling. When I scale the gameobject, the text position and scale also changes.. the text get stretched depending on the scaling direction and also the distance of the text placed from the gameobject. – yousufali.n Dec 30 '20 at 14:21
  • Yes, in this case making Canvas a child of a cube is not the best option – obywan Dec 30 '20 at 14:43
0

Thank you @derHugo. I cleaned my file and retried what you mentioned. and it is working :) :)

Posting the code below!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class dimensionScript : MonoBehaviour
{
  

    public Canvas canvas;
    
    public Text lt, wt;
    public float l, w;

    public Vector3 angles,pos,sc;


    void Update()
    {

        angles = transform.eulerAngles;
        pos =transform.position;
        sc = transform.localScale;

        Vector3 pos2 = canvas.transform.position;

        l = transform.localScale.x;
        w = transform.localScale.z;


             canvas.transform.localPosition = transform.position;
    canvas.GetComponent<RectTransform>().sizeDelta = new Vector2(l+.5f,w + .5f);
    canvas.transform.eulerAngles = new Vector3(90f, angles.y, 0);

    float rw = canvas.GetComponent<RectTransform>().rect.width;
    float rh = canvas.GetComponent<RectTransform>().rect.height;

    Vector3 pos2 = canvas.transform.position;


  lt.transform.localPosition = new Vector3(0, -rh / 2, 0);
 wt.transform.localPosition = new Vector3( - rw / 2, 0,0);
                   

        lt.text = l.ToString();
        wt.text = w.ToString();

    }



}
yousufali.n
  • 21
  • 2
  • 10