Search Unity

Mask with animated sprite ?

Discussion in '2D' started by DrSpoutnik, Sep 30, 2022.

  1. DrSpoutnik

    DrSpoutnik

    Joined:
    Jan 14, 2018
    Posts:
    72
    Hi !

    I'm doing a 2D game and my player is made with 2D sprites (legs, feet, chest, head, etc.) animated with bones in unity.

    I would like to cast on him a fake shadow : on an invisible plane, i have a sprite renderer with a black shape sprite with "mask interaction" set on "visible inside mask"

    then i tried 2 things :
    1/ on my player's sprite i added a sprite mask component.
    It works but there is a small offset because of the "sprite skin" component wich allows to distort the sprite with the bones attached.

    2/ i tried then to added a gameObject to each bones instead, with the sprite mask component. it's not really better, i have to manually overlap the sprite mask over the sprite (not so easy as it's seems) and when it's distorted by the animation, the result is quite chaotic...

    Someone would have a better idea ?
    Thanks :)
     
    Last edited: Sep 30, 2022
  2. DrSpoutnik

    DrSpoutnik

    Joined:
    Jan 14, 2018
    Posts:
    72
    After fews attempts, i think the solution 2 is still the best, even if it's a bit fastidious to align the gameObject to the sprite manually. what is 'chaotic' is when there several bones on the same sprite (ex the chest)... since the sprite mask is attache to one single bone, it won't "follow" the sprite distortion with the bones moving. one solution could to break the sprite in several parts, and keep only one bone to each part.

    Else, i would prefe a solution wich allows to use the shape of the player globally animated (not sprite by sprite, but the whole 'silhouette') as a mask... but i failed to find until now.
     
  3. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    916
    Not sure I fully understand what you are trying to do, and where it fails, but in Unity 2023.1, Sprite Mask support deformed (Sprite Skin) Sprites. But do note that this version of Unity is currently in alpha.
     
  4. DrSpoutnik

    DrSpoutnik

    Joined:
    Jan 14, 2018
    Posts:
    72
    Thanks you Ted !
    My game is 2D and i want that a "shadow" effect, like under a tree, you would have some "holes of light" in the shadow to imitate the complexity of leaves real shadows. (but it could be apply for other things, to have shadow without lights. for a more expressionist effect for instance)
    To do so, i painted a .png sprite with a shape of leaves for instance and in unity i would map it on a plane as a sprite. this shape would be invisible except over the player when he walks under it. it works good with any object or simple sprites, but with my player and his sprite distorted by the bones, it won't, or not very nicely...
    i hope i'm more clear ? :)

    i would try the 2023 version, then.
     
  5. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    916
    If that is what you are trying to do, take a look at how we did the shadows under the trees in our tech demo Lost Crypt (Blog post). It will hopefully give you some ideas on how to approach this.
     
  6. DrSpoutnik

    DrSpoutnik

    Joined:
    Jan 14, 2018
    Posts:
    72
    Thanks Ted !
    My bad, i'm still confusing : my game is in 3D not in 2D, but it's more a "2.5"D : everything is 3D objects and planes with 2D sprites mapped. i'm using 3d lights. My player is in 2D, with bones and 2D sprites.
    the example of Lost Crypt is brillant, but it uses 2D URP with 2Dlights. i could use this solution to get what i want with 2Dlights set with "sprite" projection in deed. i did a try, but it would oblige me to change my whole project in 2D URP instead.