Search Unity

Rendering order: In front of world space, but behind the UI

Discussion in 'Scripting' started by Falcoshin, Oct 6, 2017.

  1. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    What I want is fairly simple. I want to make it so that, if a player clicks on an enemy, a little targetting box appears around them. I can do this for the most part, but the problem I have is that it renders behind other 3d objects depending on their location along the Z-Axis. For instance, if I'm in front of the targetting box it will look like this:


    And if I'm behind it it will look like this:


    Almost like it's a real object in the world. While I want it to be a real object in the game world that can't be interacted with, I don't want it to LOOK like it. I want the square to render in front of everything in the scene regardless of where it is, but at the same time it has to render BEHIND everything in the UI (which shouldn't be a problem because it's not something that should exist in the UI to begin with).

    I've heard something about sorting layers, but I've tried using them and haven't been able to get anywhere with them and it seems like they're only for whether or not they're rendered in front of other 2D elements or not and not whether or not they're rendered in front of 3D elements. I've also heard a shader can fix this but I know next to nothing about shaders and would like to avoid using them if it can be helped.
     
  2. manpower13

    manpower13

    Joined:
    Dec 22, 2013
    Posts:
    140
    Not completely sure, because I don't have Unity to test it right now, I will check it tonight:

    You don't need shaders for this, it's indeed about the rendering order of the different objects. You can create a layer for this 'selection box'. Now create a second camera and make the second camera render this layer. The primary camera should not render this layer.
    The second camera should be drawn on top of the first one. That way this object will always be on top of the others.

    http://answers.unity3d.com/answers/17842/view.html gives the same solution in case my explanation was not clear.

    Every object that should then be drawn on top of everything else should have this new layer.
     
  3. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    I have no idea how to do any of that, though.
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Use two cameras. One camera renders the world objects. The other camera renders the targeting retical. You can use layers to filter out what each camera renders.

    The cameras can literally be siblings of each other (ie have the same parent) and should move together.

    Have the targeting camera always draw on top of the world camera. Then have the HUD draw last over both of those.
     
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I just realized I restated the above solutions.

    Basically, have a parent game object. Give it two children game objects. Then put cameras on each of those objects.

    When you want to move the cameras, move the parent. Both children cameras will move along with it.

    On the camera itself, if you look in the inspector there is a "Culling Mask" setting. This will tell it which layers to show.

    Each game object has a "Layer" setting on the top right. This works with the Culling Mask of the cameras.

    Cameras have a "Depth" setting which controls in what order the cameras draw to screen.

     
  6. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    Ok, that certainly did SOMETHING, but the opposite of what I want. Now the box is rendering behind the character regardless of where he is instead of in front of the character regardless of where he is.
     
  7. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    It also only applies for the character being in front of it. It still behaves normally when it's in front of other objects.
     
  8. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    Nevermind. I fixed it.
     
  9. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Using two cameras just for a red box is overkill and may cause problems down the road. A simpler way is to go to the material you're using for that box, change the shader to something like "Sprites/Default", and then change the "Render Queue" dropdown to Transparent or Transparent+1000. Then you can go back to just using one camera.
     
  10. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    It's not just for the box, but the content it's suppose to handle is fairly sparse. This is just a temporary solution so I have something to show my game meetup group. I'm planning on using a shader for this eventually because that seems like it'd be less of a headache in the long run, but I have no idea how shaders work for the time being.
     
  11. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Why not just put the targeting reticle's container in the UI below the other UI elements?
     
  12. Falcoshin

    Falcoshin

    Joined:
    May 31, 2017
    Posts:
    168
    Because it has to be able to track objects that exist in world space.
     
  13. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    That's not an issue. Just use Camera.WorldToScreenPoint and then feed that into RectTransformUtility.ScreenPointToLocalPointInRectangle to set the value to overlap the game object you want to track.

    I'm assuming you're going for something like this (12:45):
     
    andreiagmu likes this.
  14. AFlamel

    AFlamel

    Joined:
    May 20, 2021
    Posts:
    15
    Sorry for the necro but I see no crazy simple answer here.
    For the record when making a target in 3d space or some other UI element that is in the same 3D space as non-rect transforms; just remember to use World Space setting on Canvas and set the order layer on the Cavas to some large number.
    upload_2022-11-9_17-25-26.png
     
    3DGamesDeveloper likes this.