Search Unity

Highlighting Game Objects in game

Discussion in 'Scripting' started by terminal205, Jan 2, 2015.

  1. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    So I am beginning to dive deeper into my first application.
    I have a series of GameObjects that get created/instanced with new information attached to them. I would like to add some interactivity to them. Here's what I'm wanting to do:

    1: When I hover my mouse over one of the objects, I'd like it to be bracketed [ ] . I'd like it to always be facing the camera regardless of where the camera (billboard?) gets positioned/rotated in the scene. So my question is how would I do this? Would I do this with UI elements, or some other element?
    2: When I click it, the bracket changes from [ ] to a circle that behaves the same way (custom texture/material)

    Any help to point me in the right direction would be appreciated.
     
  2. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You can use LookAt to face the camera.
     
  4. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    * Sound boarding....*
    I don't think this will work. I'm trying to do something with custom textures. The LookAt option could force a 2D texture to Billboard to the camera perfectly.

    The scaling I guess could be connected to the physical distance from the object .No real work needs to be done there which is a plus.

    I think this might work. Thoughts?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Toucan use a physics raycaster with the pointer enter and exit triggers on your GameObject to detect the hover.
     
  6. REV-FX

    REV-FX

    Joined:
    Oct 24, 2013
    Posts:
    28
    No raycast necessary. OnMouseEnter and OnMouseExit in a script will do it to detect a mouse hover.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The physics raycaster with the event system plays nicer with the new UI system then OnMouseDown. Only applicable if you have 4.6 or above.
     
  8. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    Ok. So I have finally started on the part of the project and I'm already running into a problem.

    I was thinking that I could use a plane, but it doesn't render (no texture)
    I thought about using a Sprite.

    Also, I am attempting to assign images in the variable but I'm getting errors. I'm assuming Image is not a valid type. Should I be using Texture instead?

    What are your thoughts on using a plane vs using a Sprite?
     
  9. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    I discovered the quad object... this makes alot more sense to use instead of the plane...
    I still am not sure how to get the main texture to change based on onHover, offHover, and click/active states.
     
  10. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    After some searching, I found this bit of code:

    renderer.material.mainTexture = Texture;

    Let the testing commence.
     
  11. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    More questions....

    So I see i can find the number of children a transform has (Transform.childCount). Is there a way to list the children?
     
  12. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    Perhaps combining childCount and getChild?
     
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Transform is enumerable. That means you can do this

    Code (CSharp):
    1. foreach (Transform child in transform){
    2.     // Do something suitably awesome with each child
    3.     Debug.Log(child.name);
    4. }
     
  14. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    245
    Mr Terminal, use sprites!

    Check your images in the project library and you can set them to sprites instead of textures. Sprites can be placed in the Unity scene as is, without needing to apply them to any objects/geometry. They are specifically designed for putting flat/billboard images in Unity, are very well handled in code, and have more features than regular textures do!

    Edit: You can also make a [ ] sprite, child it to the normal sprite, and make it appear or dissapar when the mouse is hovered over. Nice and simple.

    If you put public Sprite thingie; in a script, then you can drag other sprites into it via the inspector and do the things such as making them visible, etc.

    Edit 2: You can also use the free iTween to make cool bounce tweens or whatever when the sprite is clicked,
     
    Last edited: Jan 26, 2015
  15. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    I got it mostly working with the Quad. I'll test out the Sprite (now that I have some of the functionality working)

    Is there a way to check if I clicked outside the colider? that way I can turn a state off?
     
  16. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    Last question of the night. I am attempting to use the for each loop to unhide/enable child transforms. I've tried a couple of different things but they do not behave as I wanted them to. (Specifically SetActive() )
     
  17. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    Think I should use raycasts to verify if I clicked on the object/transform? or is there a way that shows more finesse ?
     
  18. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    so I did some research and some testing and I came across hit.transform. I modified the example given in the documentation to do my tests, but the outcome is not what I expected. If I click on the object that has this script, it returns the transform. Perfect. Expected that.

    However, if I click outside the object nothing happens. Why is that? The logic here makes me think that if nothing is returned (null value), it should print "Missed" to the console. Obviously I messed up my logic somewhere, or perhaps I'm testing for the wrong thing?

    Code (JavaScript):
    1.  
    2. function Update ()
    3. {
    4.     if (Input.GetMouseButtonDown(0))
    5.     {
    6.         // Check to see if the object was clicked
    7.         var hit: RaycastHit;
    8.         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    9.         if (Physics.Raycast(ray, hit))
    10.         {
    11.             if(hit.transform !=null)
    12.             {
    13.                 print(hit.transform);
    14.                 //selected=false;
    15.                 //iconQuad.renderer.material.mainTexture = offImage;
    16.                 //renderer.material.color = offColor;
    17.             }
    18.             else if(hit.transform == null)
    19.             {
    20.                 print("Missed");
    21.             }
    22.         }
    23.     }
    24. }
     
  19. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Untested, but try this

    Code (csharp):
    1.  
    2.  
    3. function Update ()
    4. {
    5.    if (Input.GetMouseButtonDown(0))
    6.    {
    7.       // Check to see if the object was clicked
    8.        var hit: RaycastHit;
    9.       var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    10.        if (Physics.Raycast(ray, hit))
    11.        {
    12.            if(hit.transform !=null)
    13.            {
    14.               print(hit.transform);
    15.               //selected=false;
    16.                //iconQuad.renderer.material.mainTexture = offImage;
    17.               //renderer.material.color = offColor;
    18.            }
    19.  
    20.       }
    21.        else
    22.        {
    23.                 print("Missed");
    24.        }
    25.  
    26.    }
    27.  
     
  20. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    No change.

    It isn't really this hard to determine if I clicked the object the script is attached to is it?

    Am I perhaps going about this the wrong way?
     
    Last edited: Jan 29, 2015
  21. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Physics.Raycast returns false if it doesn't hit anything.

    Code (csharp):
    1.  
    2. if (Physics.Raycast(...))
    3. {
    4.  
    5. }
    6. else
    7. {
    8.     print("Missed");
    9. }
    10.  
     
  22. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    A little tweaking got this working.

    Moved the logic under the the OnMouseDown() call. Simply added a toggle and such to help control the overall design

    Code (javascript):
    1.  
    2. function OnMouseDown()
    3. {  
    4.    if(selected == false)
    5.      selected = true;
    6.    else if(selected == true)
    7.    {
    8.      selected = false;
    9.      iconQuad.renderer.material.mainTexture = offImage;
    10.      renderer.material.color = offColor;
    11.    }
    12.      
    13.    //var count : int = 0;
    14.    //if(selected == false)
    15.    //{
    16.      renderer.material.color = clickColor;
    17.      iconQuad.renderer.material.mainTexture = onClickImage;
    18.      for each(Transform in transform)
    19.      {
    20.        if(Transform != iconQuad.transform)
    21.        {
    22.          
    23.          //Transform.GameObject.enable = true;
    24.          
    25.          //print("Transform #" + count + " is " + Transform + "\n");
    26.          //count++;
    27.          
    28.          //menuObject.SetActive(!menuObject.active);
    29.          //transform.gameObject.SetActive(!transform.gameObject.active);
    30.        }
    31.      }
    32.      
    33.    //}
    34. }
    35.  
    36.  
     
  23. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    Now I need a way to enable the child transforms/objects of the node this script is on. See the for each loop at the end.
    I've done research but I cannot find a way to enable a transform that is disabled. Any thoughts?
     
  24. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    It seems like I need to find a way to grab the gameObject attached to the Transform during the for each loop, then use setActive() on that game object itself.

    Does this sound right?
     
  25. terminal205

    terminal205

    Joined:
    Sep 4, 2011
    Posts:
    259
    Yay! got it to work. Next stage is to visually connect my child notes back to my parent node and figure out how I want the menu items to look.

    Thank you everyone for your help!!