I’m trying to create a flexible little speach bubble attached to my player. To do this I have added a canvas to my player entity with UI Text on it. The canvas is then set to “World Space” so that it follows the player along as he moves. This works fine:
To make the player walk the other way I use a Flip() method which flips the transforms localScale
like this
void Flip()
{
faceright = !faceright;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
Problem with this is it flips my canvas and I end up with this:
How do I stop this from happening? I have tried all sorts of things and none seem to give me the correct solution. I have tried reverting the child transform locale scale by adding something like this to the Flip method
foreach (Transform child in transform)
{
Vector3 childScale = child.localScale;
childScale.x = Mathf.Abs(childScale.x);
child.localScale = childScale;
}
And I also tried adding a billboard script to the Canvas to make it look at the camera each update:
public class Billboard : MonoBehaviour
{
Camera _camera;
void Start() {
_camera = Camera.main;
}
void Update()
{
transform.LookAt(transform.position + _camera.transform.rotation * Vector3.forward,
_camera.transform.rotation * Vector3.up);
}
}
I think my anchors etc are all wrong in the canvas but I’m totally out of my depth. Anybody know what I’m doing wrong/not doing?