- Home /
Placing 3Dtext with Perspective Camera
I'm instantiating on runtime a TextMesh prefab that needs to be in the screen top-corner upon game start, with a orthographic camera (Always worked in 2D environment so I don't know anything about perspective) it's enough to use Camera.main.ScreenToWorldPoint but apparently that's not a workin option when the camera is Perspective.
The code so far :
void Start ()
{
GameObject txtMeshTransform = Instantiate(Go) as GameObject;
//TextLine is a List<TextMesh>
//ApplyToLine a List<String>
TextLine.Add(txtMeshTransform.GetComponent<TextMesh>());
ApplyToLine.Add("Hello Warudo");
TextLine[0].text = ApplyToLine[0];
txtMeshTransform.transform.position = new Vector3(
Camera.main.ScreenToWorldPoint(new Vector3(0f, 0f, 0f)).x + 1.2f,
Camera.main.ScreenToWorldPoint(new Vector3(0f, Screen.height, 0f)).y - 0.43f,
6f);
}
Try parenting the text$$anonymous$$esh to the camera and positioning it. No coding involved. This is just a guess by the way.
Is the perspective of the camera always going to be the same? (the zoom?). If so, then you can place an empty game object as a child of the camera at the location that you want. It will be your anchor for that position. Then you can make it the parent.
txt$$anonymous$$eshTransfrom.transform.parent = myAnchorGameObject.transform; //Not sure if this is the exact syntax, but it is close.
while this is true, what I'm trying to achieve is a runtime generated 3D text that can fit accordingly to different screen resolutions
Answer by GrandOpener · Jun 18, 2014 at 05:37 AM
You are misunderstanding one component of the ScreenToWorldPoint input vector. The x and y members are the screen coordinates (in pixels, measured from lower left as zero), however the z member of that vector indicates the distance in world units that the new point should be in front of the camera. Because you are passing in zero, the object is getting placed on top of the camera, which is then getting culled by the near clip plane.
What you actually want is probably something more like this:
txtMeshTransform.transform.position = Camera.main.ScreenToWorldPoint(
10f, // desired x pixel location
Screen.height - 10f, // desired y pixel location
10f); // distance from camera to text mesh
Also note: you have two choices on how to do layout: by pixel or relative. The above solution works well for "I want this text to always be 10 pixels from the border." If, instead, your layout requirements are more like "I want this text to always be 10% of the screen from the left," then you have a perfect use case for ViewportToWorldPoint (which goes from 0,0 in bottom left to 1,1 in the top right). If that's what you want, the call would look more like this:
txtMeshTransform.transform.position = Camera.main.ViewportToWorldPoint(
0.1f, // desired x viewport location
0.9f, // desired y viewport location
10f); // distance from camera to text mesh
Also note that my z value of 10 units from the camera is pretty arbitrary: I don't know what your object placement requirements are. If you want the text to be "on top" of everything else but still visible, you should consider using mainCamera.nearClipPlane as that value, to put your object at the near clipping plane. If you do this, be careful if your object has any Z depth to it (depending on the center point). You might need nearClipPlane + some value for the depth of your object.
Reference:
http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html
http://docs.unity3d.com/ScriptReference/Camera.ViewportToWorldPoint.html
http://docs.unity3d.com/ScriptReference/Camera-nearClipPlane.html
Actually works, thanks a lot for giving me also the percentage options
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to keep an object within the camera view? 2 Answers
Distribute terrain in zones 3 Answers