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 John___ · May 03, 2014 at 11:42 PM · collidertextboxcollidergui-text

Collider around GUI text

I'm making a simlpe word bazed game, and for that I'd like to have a collider around the words I'm displaying (I use GUItext for that).

I tried using a box collider, but I can't seem to find a good way to automaticly set size of the collider to match size of the word.

Code I've tried so far:

 void setCollider()
 {
     BoxCollider boxCollider = (BoxCollider) gameObject.AddComponent (typeof (BoxCollider));
     boxCollider.isTrigger = true;
     boxX = 0.1f * (guiText.text.Length - 1);
     boxCollider.size = new Vector3(boxX, boxY, 1);
 }

This function is called from the start() function. It doesn't give consistent results.

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
0

Answer by robertbu · May 03, 2014 at 11:47 PM

You can use GUIText.GetScreenRect() to get the Screen/pixel size of the text. You then need to convert that size into world space. For a perspective camera, you'll need to pick a distance in front of the camera since world size and world coordinates vary based on the distance from the camera. For an Orthographic camera, you can use the Orthographic.size to directly calculate the world size.

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 John___ · May 04, 2014 at 01:50 PM 0
Share

@robertbu
I now have this code:

 void setCollider()
 {
     Rect box = this.guiText.GetScreenRect ();
     BoxCollider boxCollider = (BoxCollider) gameObject.AddComponent (typeof (BoxCollider));
     boxCollider.isTrigger = true;
     boxX = box.width;
     boxY = box.height;
     //Debug.Log ("boxX = " + boxX + "boxY = " + boxY);
     boxCollider.size = new Vector3(boxX, boxY, 1);
 }

But the collider is way to big. I assume that is what you mean by "convert that size into world space", but how do I do that?
I'm using ortographic camera.

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

20 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

Related Questions

Read txt file into a game 1 Answer

I am trying to have a ball roll on a line that was drawn from a Line Renderer 1 Answer

Box Colliders of imported OBJ not centered on OBJ, How would i center it? 2 Answers

why isn't my OnTriggerEnter2D() function working? 14 Answers

Note Pad not flipping back 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