- Home /
Unity 4.6 Stop Canvas from flipping
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?
Answer by fafase · Feb 25, 2015 at 12:21 PM
You are flipping the parent object so anything below the hierarchy will flip with it.
Make your top object a controller object with the text mesh as one child and the sprite as another child.
Then you can independently flip things:
[SerializeField] private Transform playerTr = null; // Drag sprite object in there
void Flip()
{
faceright = !faceright;
Vector3 theScale = playerTr.localScale;
theScale.x *= -1;
playerTr.localScale = theScale;
}
Ok, I take it I would need to move all my physics etc to the parent object too so that that everything moves together? Thats a pretty good idea!
Well that is how I tend to organize my coding, top object holds the controllers of the objects, sub objects will be either controlled by the top controller or some scripts that are specific to that object at the level.
In your case, I would have an Input controller that passes the data to some controller at the parent level and pass down the info to the appropriate child if necessary.
That also means your flip method is on a script on the child object and that method is called from above either via a direct reference (not so happy but well) or via an event (real happy yeah).
I followed this instructions, and now my character doesn't have animations (yes i transfered the animator to the parent object) and it can jump infinitly
Answer by aadyainfotech · Dec 23, 2015 at 04:32 PM
@CrimsonChin: You can keep your setup as it is. Just add a transformation to your canvas in your script same as the players transformation:
Also remember to flip back the canvas along with your player.
public GameObject canvasBoard; // variable
transform.localScale = theScale;
canvasBoard.transform.localScale = new Vector3(-1,1,1) // add this after your player transform.