Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Tutorial] Simple Way to Outline 3D Objects with OnMouseOver - Begginers/Intermed.

Discussion in 'Scripting' started by frozendog_bren, Aug 24, 2016.

  1. frozendog_bren

    frozendog_bren

    Joined:
    Nov 29, 2014
    Posts:
    19
    Hey, devs. My name is Breno Oshiro and i'm currently working on this project: http://brenooshiro.itch.io/aron doing animations, some scripting and game design. The version available is a bit outdated from what we have now, but anyways, i recently added a "feature" that i couldn't easily find on the internet and some of them were old. It's a simple way to outline the enemies we can select as targets (or interactible objects), just like on point-and-click-like games... Also, if you find anything interesting in this game i'd be glad to help. Just let me know!

    -- to the point --

    It's pretty easy, if you play the game, you see a black sprite on the ground right below the enemy everytime you pass/rest the cursor over them. It is a game object with a sprite that i set active using the OnMouseOver and inactive with OnMouseExit.

    Like this:
    Code (CSharp):
    1. public GameObject targetSelectedGO;
    2.  
    3. void OnMouseOver()
    4. {
    5.           targetSelectedGO.SetActive(true);
    6. }
    7.  
    8. void OnMouseExit()
    9. {
    10.           targetSelectedGO.SetActive(false);
    11. }
    This piece of code is in a script called Enemy_VisualFeedback attached to every enemy in the game. I'm using it to trigger particle effects and other things. A important thing to remember is that this specific script must be attached to all those objects you want to interact with and highlight.


    -- doing it --

    With that said, here comes the hardest part. First you download the files i've uploaded here (Outliner_Material&Shader.rar), extract and import into Unity. To outline something you just have to apply the outliner material (default shader: outliner/silhouette only) to the object, but you'll notice that it will delete all the other materials making your mesh invisible. Using this "problem", to outline just when the mouse is over, i duplicated my character mesh and applied the outliner material. Then the last step was to drag and drop the object to make it a child of the targetSelectedGO (the game object with the black sprite) which is inside the interactible object in question and finish setting the stroke color and width.

    Now everytime the game object with that black sprite (currently the red circle seen in the images) gets active, the mesh with the outline material will show up too. And good news, the duplicated mesh is still getting information from the rig and following the animations!


    Make sure to turn off cast and receive shadows from this mesh, you don't need to process this extra stuff.

    -- the last thing, a problem --

    Ok. The only problem i found here was: once you apply the material to other objects and meshes, all of them will get the same settings, meaning the same color and width. This might not work to every asset you want, some of them may look better with a smaller/higher width value or a different color and so on. To overcome this i just created new materials with the same SHADER, but different settings.



    Easy, right? I hope it was helpful!
    Any tips or questions, leave a comment.
    Thanks!


     

    Attached Files:

  2. dincicode

    dincicode

    Joined:
    Feb 8, 2012
    Posts:
    7
    The good thing is all the images are unlinked. lol!
     
    rg1693 likes this.