Center TextMesh to 3DModel

Hey guys,

we try to center a 3Dtext (textmesh) to a 3DModel while changing the camera. At the moment we have a code like this:

descriptionText.transform.position = b.GetComponent<Transform>().position
                                     + new Vector3(0,10,0);
Camera.mainCamera.WorldToViewportPoint(descriptionText.transform.position);
descriptionText.transform.rotation = Camera.mainCamera.transform.rotation;

At first we change the position of the textmesh to the 3DModel, then we change the viewport and rotation of the textmesh, so it looks at the camera. Now we have the problem, that the text begins at the centre of the 3DModel, thus the text is shifted (depends on camera position and rotation).

Does anyone have a solution for this problem?

What exactly are you doing with that Camera.mainCamera.WorldToViewPortPoint? You call the function but never use the returned value.

If your TextMesh component is anchored at the center, then simply setting the position of the TextMesh to the position of the object would do the trick.

//Also, since all components can access the inheritted property transform,
//you don't need the GetComponent.

descriptionText.transform.position = b.transform.position;
descriptionText.transform.LookAt(Camera.mainCamera.transform,
                       camera.mainCamera.transform.up);

If you are talking about GUIText components, then those would use viewport coordinates and ignore rotation.

descriptionText.transform.position =
    Camera.mainCamera.WorldToViewportPoint(b.transform.position);