- Home /
Text with fixed size in viewport
Hi, i'm trying to do my Text Mesh always have the same size in the viewport. However, something weird is happening. The text size change all the time, with a strange behavior. It's get bigger and than smaller, repeatedly.
My code is this:
void Update()
{
TextMesh textMesh = _billboard.GetComponentInChildren<TextMesh>();
TextBackground textBcg =_billboard.GetComponentInChildren<TextBackground>();
if(textMesh)
{
_targetViewportHeight = 40;
textMesh.fontSize = 40;
Vector3 minProjPoint = _camera.GetComponent<Camera>().WorldToScreenPoint( textMesh.collider.bounds.min );
Vector3 upperLeftCorner = new Vector3( textMesh.collider.bounds.min.x, textMesh.collider.bounds.max.y, textMesh.collider.bounds.max.z );
Vector3 maxProjPoint = _camera.GetComponent<Camera>().WorldToScreenPoint( upperLeftCorner );
float scaleCorrection = _targetViewportHeight / Mathf.Abs( maxProjPoint.y - minProjPoint.y );
_billboard.transform.localScale = new Vector3( scaleCorrection, scaleCorrection, scaleCorrection );
}
}
This is what I get from my code, in play mode.
The Text Mesh get bigger and then smaller, repeatedly. I don't know how to fix this, or what I'm doing wrong.
What seems to me (via debug tests), is that changing the scale doesn't affect the bounds. Bounds are not been update. =S
Thanks for any help.
It would be simpler if you just used a GUIText object ins$$anonymous$$d.
Yeah, but, besides this, the text have to "follow" the camera, so I thought I have to use Text $$anonymous$$esh, so that the text would be into the world. Can I do this with GUI Text?
Do you have a perspective scene? In other words, a 3d scene?
Answer by paulaceccon · Oct 30, 2012 at 08:32 PM
Hi, I could do this work with the following code:
void Update ()
{
TextMesh textMesh = _billboard.GetComponentInChildren<TextMesh>();
TextBackground textBcg = _billboard.GetComponentInChildren<TextBackground>();
if(textMesh)
{
_targetViewportHeight = 40;
Vector3 minProjPoint = _camera.GetComponent<Camera>().WorldToScreenPoint( textMesh.renderer.bounds.min );
Vector3 upperLeftCorner = new Vector3( textMesh.renderer.bounds.min.x,
textMesh.renderer.bounds.max.y,
textMesh.renderer.bounds.max.z );
Vector3 maxProjPoint = _camera.GetComponent<Camera>().WorldToScreenPoint( upperLeftCorner );
float scaleCorrection = _targetViewportHeight / Mathf.Abs( (maxProjPoint.y - minProjPoint.y) );
_billboard.transform.localScale = new Vector3(_billboard.transform.localScale.x * scaleCorrection, _billboard.transform.localScale.y * scaleCorrection, _billboard.transform.localScale.z * scaleCorrection);
}
}
In others words, the problem was at the last line, cause I was not considering the previous scale.
Your answer
Follow this Question
Related Questions
Placing a GameObject on top of another GameObject. 1 Answer
Changing two different objects renderer colour 1 Answer
Can't convert Bounds from world coordinates to local coordinates 6 Answers
Get right or left Vector2/3 out of renderer.bounds 0 Answers
Want to frame multiple objects in camera view using render bounds 0 Answers