• 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
0
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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 1 Answer

Adding vertices to TextMesh Pro 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges