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
2
Question by elpedro_75 · Apr 17, 2014 at 01:22 AM · texttextmeshbox collidertext mesh

Fitting a box collider to 3D text perfectly

Hello,

This question has been posed numerous times in various contexts, but I do not know that a solution has ever been found. I have not encountered one.

I have a menu with 3D text, and I have a box collider of nominal dimensions to detect mouse events (hover and click). So far, so good, except that I would like to size my box colliders according to the size of the text, not nominally. The text itself comprises a mesh, and the most obvious solution would be to read the size of the text from the top left vertex to bottom right vertex, then resize the box collider accordingly. However, I cannot find a way to do this.

Does anybody know how? I am using C#.

Thank you in advance,

Peter

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 MikeNewall · Apr 17, 2014 at 02:34 AM

Try this. I'm lazy so I just put the code in OnDrawGizmos.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(BoxCollider))]
 public class ResizeCollider : MonoBehaviour{
 
     public float paddingX;
     public float paddingY;
 
     void OnDrawGizmos(){
         BoxCollider col = (BoxCollider)collider;
         col.center = new Vector3(renderer.bounds.extents.x, renderer.bounds.extents.y - renderer.bounds.size.y, transform.position.z);
         col.size = new Vector3(renderer.bounds.size.x + paddingX, renderer.bounds.size.y + paddingY, 1);
     }
 }
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 elpedro_75 · Apr 17, 2014 at 09:12 AM 0
Share

Thank you for your solution, $$anonymous$$ike.

After a few small modifications, your solution worked perfectly. Line 11 threw a casting error, so I replaced that line and RequireComponent with an AddComponent statement. I also altered the calculations slightly to account for my Text $$anonymous$$esh anchor's being at middle centre. In the end, my code read:

     void SizeCollider()
     {    
         BoxCollider boxCollider = (BoxCollider) gameObject.AddComponent (typeof (BoxCollider));
         boxCollider.center = new Vector3 (renderer.bounds.extents.x - renderer.bounds.size.x / 2, renderer.bounds.extents.y - renderer.bounds.size.y / 2, transform.position.z);
         boxCollider.size = new Vector3 (renderer.bounds.size.x + TEXT_PADDING_X, renderer.bounds.size.y + TEXT_PADDING_Y, 1);
     }

Interestingly, the bounds property automatically includes the space between letters, and the space below and above the line which would accommodate hanging letters (g, j, y) and accented capitals (for example, Á) even if they are not present. The latter makes sense in this context because I want my colliders to be of equal height, regardless of the letters that the text includes.

avatar image MikeNewall · Apr 17, 2014 at 10:12 AM 0
Share

Thanks for the updated code. Didn't have a chance to test it properly. Can you accept the answer so other people can see the solution? :)

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

22 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

Related Questions

TextMesh Pro visible in editor but not in game window. 2 Answers

How to find position of sprites generated by tag in TextMesh Pro? 0 Answers

Text Mesh Pro: highlight all words in a link 0 Answers

TextMesh Pro Thicker Wider Outlines 2 Answers

Change TextMesh text in script 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