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 :)
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();
}
}