- Home /
Moving a canvas relative to a gameobject's position
Im making an RTS game where you can click to buy buildings then place them but I having trouble when rotating the building. It has a canvas that displays the health of the building and when I rotate the building it rotates relative to the building too. How can I make the canvas rotate so that it basically stays in the same spot nomatter the rotation (See the picture). Thanks!!
This is what I've been trying:
It needs to go through these points
1 (0, 24.3, -32)
2 (32, 24.3, 0)
3 (0, 24.3, 32)
4 (-32, 24.3, 0)
Then just repeats after that.. It is the fourth point that keeps messing me up because if you I cant get the third point to get to the fourth point. How can I do this?
currentBuilding.GetComponent<PlaceableBuilding> ().Bars.transform.localPosition = new Vector3 (-currentBuilding.GetComponent<PlaceableBuilding> ().Bars.transform.localPosition.x + 32, 24.3f, currentBuilding.GetComponent<PlaceableBuilding> ().Bars.transform.localPosition.z + 32);
Answer by SmomoGame · Dec 30, 2016 at 09:48 AM
canvas.transform.position = building.transform.position + new Vector3(x,y,z) // Set world position via script, this position is "relative to" building
canvas.transform.LookAt(Camera.main) ; // Make canvas always look at the camera
canvas.tranform.eulerAngles = new Vector3(x,y,z) ; // Or set a constant rotation you wish
Try set world position and rotation in Update(), it will override the canvas' local transform.
Your answer
