Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 xproject2013 · Aug 31, 2013 at 03:11 PM · cameraguiworld

Problem making a ruler

Hello, I'm trying to create a ruler by C# scrip, the proble is that the labels of the numbers are not put in correspondence with the rulers, this is because the rulers are GameObjects that have world coordinates and the labels that are guiText attached to the gameObject having as coordinates the pixeloffset.

Below how is the result of the script and the script itself, someone can help me? Thanks in advance!

alt text

 using UnityEngine;
 using System.Collections;
 
 public class GridController : MonoBehaviour {
 
     public float stepSize = 0.05f;
     public GameObject line, ruler;
     
     
     void Start(){
         rulerInit();
     }
     
         public void rulerInit(){
         
         
         // delete previous ruler
         if (GameObject.FindGameObjectsWithTag("Ruler") != null) {
             GameObject[] Rulers = GameObject.FindGameObjectsWithTag("Ruler");
             
             foreach (GameObject cancelRule in Rulers){
                 Destroy(cancelRule);
             }
         }
 
         
         float
             xRight = 0f,
             yTop = 0f;
         
         
         
         for (int i=0; i < 50; i++){
         
             if (i > 0) {
                 xRight += 0.2f;
                 yTop  += 0.2f;
             }
 
             
             Vector3 hPosRight = new Vector3 (xRight, -0.2f, 0);
             Vector3 vPosTop = new Vector3 (-0.2f, yTop, 0);
             //Debug.Log (hPosRight);
             
             // label
             if (ruler.GetComponent("GUIText") == null) {
                 ruler.AddComponent("GUIText");
             } 
             
             ruler.tag = "Ruler";
             ruler.guiText.richText = true;
             ruler.guiText.fontSize = 14;
             ruler.guiText.lineSpacing = 0;
             ruler.guiText.color = Color.black;
             ruler.guiText.transform.localScale = Vector3.zero;
             
             
             //ruler.guiText.transform.position = rulerLabelPos;
             ruler.guiText.anchor = TextAnchor.MiddleLeft;
             ruler.guiText.alignment = TextAlignment.Left;
             ruler.guiText.lineSpacing = 0f;
             ruler.guiText.text = i.ToString();
 
             Quaternion rotation = Quaternion.identity;
 
             Vector3 hScale = new Vector3 (0.01f, 0.1f, 0.01f);
             ruler.transform.localScale = hScale;
             ruler.guiText.name = "ruler_horizontal_"+ i.ToString();
             ruler.transform.position = hPosRight;
             
             Vector3 point = new Vector3 (ruler.renderer.bounds.max.x, ruler.renderer.bounds.max.y, 3f);
 
             Vector3 pointOnScreen = Camera.main.WorldToScreenPoint(point);
             Vector2 pixelOffset = new Vector2(Mathf.RoundToInt(pointOnScreen.x), Mathf.RoundToInt(pointOnScreen.y)+30);
             ruler.guiText.pixelOffset = pixelOffset;
             
             Instantiate(ruler, hPosRight, rotation);
         }
     }
 }

ruler.jpg (3.1 kB)
Comment
Add comment
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

1 Reply

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

Answer by robertbu · Aug 31, 2013 at 05:02 PM

Why not use 3D Text instead of GUIText? I'm sure sure how you are doing all your numbers. Near as I can see the above code only does a single number. But to answer the specific question, GUIText lives in Viewport space. Set the Pixel Offset of the GUIText to (0,0). Then you position the text using Camera.WorldToViewportPoint(). Assuming 'point' is the position for the number (and you've setup the anchor the way it needs to be):

 ruler.guiText.transform.position = Camera.main.WorldToViewportPoint(point);
Comment
Add comment · Show 2 · 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 xproject2013 · Aug 31, 2013 at 08:02 PM 0
Share

Thanks a lot!!

avatar image Keshar xproject2013 · Aug 24, 2018 at 02:50 PM 0
Share

Did you get the solution bro and if yes can you share the same as i am also trying to make a ruler.

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

17 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

Related Questions

I want a HitMark(GUI) system like in Crysis-CoD that indicates where i am hit from and also a Camera/Screen shake effect 3 Answers

Unity2D - Camera in exported game is zoomed out compared to unity game preview HELP PLEASE! 2 Answers

Convert world space to GUI Rect? 5 Answers

Control the scene camera using arrow gui buttons 0 Answers

How do I mask off specific parts from a plane or cube? 3 Answers


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