- Home /
3D text as a button
Hello, sorry if this seems a little bit noobish, but I am just starting to begin using Unity. I currently have 3d text displayed on the scene, but I want to use this as a button. I've tried raycasting and I can't seem to get it to work. Are there any alternatives to raycasting, or if not, how would I correctly use raycasting to create a button. Also, if possible, can you also state how you would make it so when you hover over the text, a light turns on, ect?
hey bud and Welcome to Unity Answers, With Text $$anonymous$$eshes, which i assume you are using, you may access the On$$anonymous$$ouseOver functionality.. Im going to link you to ansswer I once gave here for the same question, practically. I gave them a script that carries out what you are asking for, so follow that link bud. Changing around and interacting with text meshes answer
To raycast, follow any instructions for getting a mouse-position raycast to anything in the game world, like a cube. Raycasts go to invisible colliders (or triggers,) and don't care about what we see, like red cubes or 3D words. You're probably having trouble with that part. Once it works, just stick the 3DText there, and hide anything else.
But, yes, as Gruffy & Eric555 write, using On$$anonymous$$ouseDown is easier (but, you still have to figure out colliders. They tell the game where something counts as being.)
Answer by Gruffy · Mar 29, 2014 at 02:40 PM
Okay, so just in case you decided not to follow my link above in the comment. I have just created a package that you can download zip , unzip it, import package etc.
here it is below, it does not use raycasts, as there isnt really a need to do so. It uses the unity process of material swapping on the text meshes using access to the OnMouse"" events automatically fired by Unity Engine along side the likes of Update, Start, Awake etc.
Soo.... if you download this [zip][1] Then follow these below steps. 1: Extract zip file contents(.package) to your desktop (or somewhere convenient) 2: Inside Unity, "Import Package-> Custom Package" 3: navigate to the freshly unzipped .package file and select it. 4:Make a "New Scene" and drag the "GUIControlObject" prefab on to your camera. 5: Press Play Button 6: move your mouse about and click on the 3 3D texts you see there.
7: Go to Code, see how that works, I have commented it. 8: Look at the text meshes and click on their respective fields in the inpsector to see the material attached to it etc. 9: rest easy, knowing today was a good day. Take care bud Gruffy [1]: /storage/temp/24478-textmeshmouseovers.zip
Thank you so much for going that extra step to help me. Thanks for being so helpful :)
Answer by Eric5h5 · Mar 29, 2014 at 01:46 PM
Put a collider on it and use OnMouseDown. Don't bother with manually raycasting.
Answer by _1 · Mar 29, 2014 at 01:25 PM
So to accomplish making 3d text as buttons you were very much on the right track with using ray casting, what you would want to do is create a ray from the camera that will detect your text
#pragma strict
var LightSource : Light;
function Update()
{
var ray : Ray = camera.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray, hit))
{
if(hit.collider.name == "InsertNameOfYourButtonHere")
if(Input.GetMouseButton(0))
{
//Do what you want here
}
}
}
This creates a ray from the camera to the mouse and if the ray hits your text (make sure it has a collider, doesn't usually have one) and the Left mouse is pressed it will do what ever you assign it to do, now you said something about turning lights on and off, thats easy wants you have this code in, all you do is
if(hit.collider.name == "InsertNameOfYourButtonHere")
{
LightSource.light.enabled = true;
}else{
LightSource.light.enabled = false;
}
Hope this helps!
Best of luck :)