Search Unity

Question Cinemachine zoom AND DRAG zoom transparency?

Discussion in 'Cinemachine' started by AerionXI, May 11, 2022.

  1. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    Hi all. I am trying to fade my object's transparency in / out depending on how close the Cinemachine camera is to the target? Can I see an example using the Free Look?

    Thank you!
     
    Last edited: May 11, 2022
  2. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    Look at our FadeOutNearbyObjects sample scene.

    Window -> Package Manager -> Cinemachine -> Import samples.
    Screen Shot 2022-05-04 at 5.06.58 PM.png
     
  3. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    @gaborkb : I know how to do that already. What I'm SPECIFICALLY asking is how to add TRANSPARENCY to the player object depending how close or far the Cinemachine camera is to the target when zoomed in / out OR the camera drags across the floor and zooms in or out. I don't want to fade in / out OTHER objects, I want to fade in / out the PLAYER if that makes sense. Much appreciation!
     
  4. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    P.S. I am not seeing ANY example through the search tool in Unity, " FadeOutNearbyObjects"
     
  5. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    FadeOutNearbyObjects sample uses a shader to fade out materials based on the camera's location.
    In the sample scene, click on FreeLook Camera. At the bottom of its components, you'll find CinemachineFadeOutNearbyObjects script. This script has a couple of parameters that you can use to fine-tune this effect.
    By default, this script is using the FadeOut material, but you could change it if you want. By default, this material is used by all the objects in the scene, but the player.
    You can assign this material to your player and then your player is going to fade out depending on the camera's location.
     
    Gregoryl likes this.
  6. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    In Unity, on the top bar: Window -> Package Manager -> Cinemachine -> Import samples.
    This is going to import a Samples folder into your project. In there, you'll find Cinemachine -> "version number" -> Cinemachine Example Scenes -> Scenes -> FadeOutNearbyObjects.
     
    Gregoryl likes this.
  7. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482

    Attached Files:

  8. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    Can someone help? I do not see the example file as in the above screenshot.... :(
     
  9. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    The FadeOut sample scene is in CM 2.8. You need to update your cinemachine from 2.6.11 to 2.8.X.
     
  10. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    @gaborkb : Right, I've got it now. But how do I apply this to the player and not the objects?
     

    Attached Files:

  11. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    See this message above.

    Play around with the parameters a bit, and you'll get a feel how it works.
     
  12. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    Issue partially solved. So I have a player. On that player is a visor. So now, the problem is that the player fades out fine, but the visor is still partially visible. When the visor becomes near-transparent, it is hard to see but it is there ( which it shouldn't be ). Also, when I get close enough, part of the visor just blinks out of existence as you see in this short video clip. How do I fix these 2 final issues? I've got 1 Material for the Player itself & the same type of Material except with a white color for the children ( which could be the visor, etc ), how do i ensure the whole player object and all of its children ( despite what color they are ), fade into obscurity and when close enough, prevent part of the visor from blinking in and out of existence depending on how close or far the camera is from the player? Much thanks for your help!
     
    gaborkb likes this.
  13. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    You need to assign a material that fades out to all of them. You could extend our script to have more than 1 material that fades out. Or you could add a script to your player that assigns the fadeout material to all its children.

    The second one is a bug in the shader. We'll fix it.

    In the meantime, you can also fix the shader:
    To edit the shader, click on Edit on the material next to shader selected.
    Screen Shot 2022-05-13 at 2.15.52 PM.png

    Then in the shader script, write ZWrite Off instead of ZWrite On.
     
    Gregoryl likes this.
  14. namcap

    namcap

    Joined:
    Jan 8, 2016
    Posts:
    49
    Is there a URP version of this shader? It seems to only work with the standard rendering pipeline since it's a surface shader.
     
  15. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    namcap likes this.
  16. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    Is there not an easier way to fix this transparency with the children issue? I am a programmer but I only write game-based scripts, not shaders.
     
  17. AerionXI

    AerionXI

    Joined:
    Jul 20, 2020
    Posts:
    482
    Someone can help? :( I need a way to apply this shader to all children of an object...
     
  18. gaborkb

    gaborkb

    Unity Technologies

    Joined:
    Nov 7, 2019
    Posts:
    856
    I think this is what you need. Attach this to the parent gameobject, and it will assign the shader to them. Probably. Have not tested.
    Code (CSharp):
    1. foreach (var material in go.GetComponentsInChildren<Material>())
    2. {
    3.        material.shader = myShader;
    4. }