Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to use transform.position.z to decide model render result?

Discussion in 'Scripting' started by cwbeta, Jul 13, 2017.

  1. cwbeta

    cwbeta

    Joined:
    Jan 12, 2017
    Posts:
    30
    I'm making a 2d platform game which use 3d monsters, when monsters gather together, they just pass through each other. I want to render the monsters just using their transform.position.z, but still need to keep the origin 3d light effect upon the models.

    It's a bit hard to describe the question clearly, so just watch the pictures. Just imagine the cube and the capsule as 2 different monsters.
    In scene window, it looks like:
    QQ截图20170713173240.png
    In camera, it looks like this:
    QQ截图20170713173248.png
    the Cube's transform.position.z is in front of the capsule. Is it possible to render like the picture below?
    I know it's possible to make it with 2 cameras, but there might be a lot of monsters. It's impossible to create a lot of cameras.
    QQ截图20170713173316.png

    Anyone has some advice? Thank you!
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    you may be able to modify the render order with https://docs.unity3d.com/ScriptReference/Material-renderQueue.html

    also of note, if you are using directional lighting, and an ortho camera, you should be able to move your entities forward and backwards without affecting the lightning or rendering , and avoid this problem wntirely (if you have point lights this wont work)
     
  3. cwbeta

    cwbeta

    Joined:
    Jan 12, 2017
    Posts:
    30
    Thank you for your reply!
    I remember that render order only affects the order when rendering but does not affect z-write? I tried setting the render order in render queue manually, but 3d objects with different render order still pass through each other.
    Maybe I did something wrong? Could you show me an example which works as the 3rd picture I post?

    I'm using an perspective camera though, and point lights are necessary for some effects:(...

    I guess a perfect solution for me is a shader which normally calculate the lighting, and then show gameobjects ordered by transform.position.z...However I'm not sure if it's possible.