- Home /
Dynamic Collidersize based on string length for '3d Text'?
Problem: I am looking to get the length of a '3d Text' object, and to adjust a box collider based on this lenght.
Context: I have implemented a modified version of This Drag-And-Drop-Script, that have been working fine whilst using a simple box collider. What I need is a dynamic manipulation of the box collider based on the length of my 3d Text object.
I am spawning individual words in a sentence, as a list of strings that each hold one word, and I want these to be draggable. Of course with the best possible interaction, being that the collider fits the word somewhat perfectly. And the spacing between the words fitting that of a normal sentence (this should be easily solveable when I get a fitting collider).
Approaches so far: I have tried using the .Length property, however this does not take the different "length" of individual letters into account. I could take the largest posible length of a letter, the smallest posible, or an estimated average of the length of all letters in the alphabet and use this as a factor for the .Length property. These will however result in weird spacing between individual words, as these are to be placed based on the same value as the colliders length.
MeshCollider wasn't a solution either as the 3d Text does not have an actual mesh.
Thanks in advance.
EDIT:
The solution resulted in the wanted collider and wanted spacing, as illustrated on the picture below.
(The words are in danish, so don't mind the meaning of them.)
Gonna check out renderer.bounds.size as suggested by Shrandis and Jim. Hope this is what I'm looking for so far.
It was indeed what I needed, one up to both Shrandis and Jim. And, as expected, I will be able to place the individual words with the right spacing based upon the same method.
It even works better than expected. Would really like to 1up more than once. Thanks a lot.
Hi can you post the solution for creating individual 3d Text with collider for each word, im making a 2d game that the user click and drag some words to complete the phrase, but im having trouble to set the collders in the right position, could you help me?
Answer by Shrandis · Nov 27, 2012 at 02:33 PM
You can set the collider's size to renderer's bounds. Example:
BoxCollider col = (BoxCollider)collider;
col.size = renderer.bounds.size;
However, the collider's position won't fit the text unless the text's anchor is middle-center. So if your text has another anchor, you'll need to change the collider's position too. Depending on what you need you might want to just set the collider's X or Y or Z instead of all 3 of them.
Please don't forget to click the checkmark & thumbs up if my answer answered your question and feel free to add comments if you have questions.
;) Not going to forget. I'll will check if this is the solution I need. I'm used to the "answers" Q&A format from stackoverflow, and do very much intent to give credit where it is due.
Answer by loopyllama · Nov 27, 2012 at 02:35 PM
try fetching the renderer.bounds of a word then setting the box collider using the bounds info
maybe that will be accurate enough for your needs
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
[4.6 - UI] Changing the text component via script? 6 Answers