Search Unity

3d text as a button...

Discussion in 'Scripting' started by hosse, Mar 16, 2011.

  1. hosse

    hosse

    Joined:
    Feb 22, 2011
    Posts:
    61
    Hi all, really simple task here. What else do I need to get my 3D text to behave as a button..? I have this code on the text, its highlighting fine, do I need some kind of collision on the text itself?

    Code (csharp):
    1. var highlightColor = Color.yellow;
    2.  
    3. function OnMouseDown () {
    4. renderer.material.color =  highlightColor;
    5. Application.LoadLevel ("MainApp");
    6. }
    7.  
    8. function OnMouseUp () {
    9. renderer.material.color = Color(0,0,0);
    10.  
    11. }
    12.  
    13. function Start () {
    14. var www = WWW ("http://server.co.uk/someasset_01.unity3d");
    15. yield www;
    16. // Get the designated main asset and instantiate it.
    17. Instantiate(www.assetBundle.mainAsset);
    18. }
    thanks
    -hosse
     
    Last edited: Mar 16, 2011
  2. synapsemassage

    synapsemassage

    Joined:
    Jul 27, 2009
    Posts:
    334
    just add a collider and use raycast
    something like this:


    Code (csharp):
    1. private var hit : RaycastHit;
    2. private var ray : Ray;
    3. private var HitGameObject : GameObject;
    4. static var PlayerCamera : GameObject;
    5. public var rayDistance : float = 100;
    6.  
    7. function Awake()
    8. {
    9.     PlayerCamera = GameObject.Find("Main Camera");
    10. }
    11.  
    12. function Update ()
    13. {  
    14.     ray = PlayerCamera.camera.ScreenPointToRay (Input.mousePosition);
    15.    
    16.     if(Input.GetMouseButtonDown(0))
    17.     {
    18.         if (Physics.Raycast (ray, hit, rayDistance))
    19.         {
    20.             HitGameObject = hit.collider.gameObject;
    21.         }
    22. }}
     
  3. hosse

    hosse

    Joined:
    Feb 22, 2011
    Posts:
    61
    hi there,
    Thanks for the reply.! I can't figure out where to put my code in this raycast script to get my level to load.... can you help.. i just get errors.! I have a collider on the 3d text.. just not sure where this code needs to be entered,.

    Code (csharp):
    1.  
    2. Application.LoadLevel ("MainApp");
    3.  
     
    Last edited: Mar 16, 2011
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You don't need to use raycasting; just a box collider and OnMouseDown works fine.

    --Eric
     
  5. hosse

    hosse

    Joined:
    Feb 22, 2011
    Posts:
    61
    Thanks Eric, Raycasting wasn't needed.! Thanks to synapsemassage, I got your code working too.

    My new problem is getting my assetbundle to load OnMouseDown... this is what I have so far. I also don't fully understand the instantiate part either..

    Code (csharp):
    1.  
    2.  
    3. var highlightColor = Color.yellow;
    4.  
    5. function OnMouseDown () {
    6. renderer.material.color =  highlightColor;
    7. }
    8.  
    9. function Start () {
    10. var www = WWW ("http://hosse.co.uk/someasset_01.unity3d");
    11. yield www;
    12. var progress : float
    13. // Get the assetbundle and instantiate it.
    14. Instantiate(www.assetBundle.mainAsset);
    15. }
    16.  
     
    Last edited: Mar 17, 2011