Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rphysx · Jun 17, 2014 at 11:06 PM · c#cameraperspective

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);
     }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SteelArrow21 · Jun 17, 2014 at 11:08 PM 0
Share

Try parenting the text$$anonymous$$esh to the camera and positioning it. No coding involved. This is just a guess by the way.

avatar image AndyMartin458 · Jun 18, 2014 at 12:48 AM 0
Share

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.
avatar image Rphysx · Jun 18, 2014 at 01:39 AM 0
Share

while this is true, what I'm trying to achieve is a runtime generated 3D text that can fit accordingly to different screen resolutions

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rphysx · Jun 18, 2014 at 08:09 PM 0
Share

Actually works, thanks a lot for giving me also the percentage options

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Drag And Drop with Perspective Camera 1 Answer

Character Move Forward after Camera Perspective 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges