Search Unity

Question Make 2D outline appear around player animator rig?

Discussion in '2D' started by anikataylor, May 7, 2023.

  1. anikataylor

    anikataylor

    Joined:
    Jan 15, 2022
    Posts:
    44
    Hey so I have a 2d player character game object. This game object has many child game objects that make up a player rig, with many sprite renderer components. The animator component is in the main parent game object for the player which controls the animations of the player rig etc.

    I would like a 2D outline to appear around my player (at a certain point in my game), but I have no idea how I will be able to apply it. I have a 2D outline shader that works fine for individual sprite renderers but I don't know what to do for my player.

    If I were to apply my outline shader to each individual sprite renderer then I'm almost certain I'd get outlines for each part of my player (arms, body, legs, etc), which isn't what I want, I just want an outline around the whole player not each body part.

    So I'm not sure what to do. Is there a way perhaps to get a current sprite of the whole player at the current moment in the game from the Animator perhaps which I can use? Please can someone help me out thanks.
     
  2. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    see this solution to merge all the parts of your sprite, by @Kurt-Dekker

    https://forum.unity.com/threads/fea...e-sprite-renderer-group.1317972/#post-8337993

    unfortunately unity doesnt offer any features out of the box to merge sprites, its also a problem if you try to make your sprite transparent its gonna look like a mess, it will be obvious that the sprite is made of many parts, its very sad
     
    Last edited: May 8, 2023
    anikataylor likes this.
  3. anikataylor

    anikataylor

    Joined:
    Jan 15, 2022
    Posts:
    44
    Thanks again for a solution. However while merging all parts of my sprite seems like what I would want, I downloaded the unity package attached to the solution you gave me, and I couldn't find where this was done. Maybe I'm being stupid but I looked at the scripts and I couldn't find an instance where this was done.
     
  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    atm I cant open the package, maybe @Kurt-Dekker can elucidate?

    However how I understand it here is what you would do:

    1 - create a camera that using layers ignores everything in your screen except for your character and all its parts, so the camera is only rendering your character. Make sure that the camera ignores the skybox and renders with a transparent background

    2 - reduce the bounds of the camera to frame your sprite, leaving enough room for the sprite outline

    3 - send the results of that camera to a render texture asset

    4 - you probably need to modify the shader to accept the render texture on your texture2d samplers, im unsure about this

    5 - if you want to use sprite renderer i think you would need to use sprite.create to create a spritesheet of all your sprite animation frames, but since it seems you are using a skeleton i assume your animations are rather fluid and they would have too many frames
     
    anikataylor likes this.
  5. anikataylor

    anikataylor

    Joined:
    Jan 15, 2022
    Posts:
    44
    Thank you this seems like a solution that can get me on the right path to figuring this problem out. I've been attempting to follow this for a few hours now and have come across two problems.

    1 - I'm struggling to make it so that my camera ignores the skybox and renders with a transparent background. I've made it so that my camera only renders my player but I don't know how to how to make a transparent background. I've set the Background Type on my camera component to Solid Color, and I was thinking that sliding down the Alpha property to 0 would do the trick, but moving the Alpha slider doesn't do anything.

    2 - I did what you said and modified the shader to accept the render texture which in the shader graph shows my character to have a nice outline. However I have no idea how I will apply this to my player. I have created a new child game object for the Player called Player Outline, and I tried adding a Sprite Renderer + setting the material to a material with my new modified shader graph, but that doesn't work (I'm guessing because there is no sprite set in the sprite renderer). So I have no clue how I can apply this shader to my player to get an outline.
     
  6. anikataylor

    anikataylor

    Joined:
    Jan 15, 2022
    Posts:
    44
    has anyone got any solutions to my question? sorry to sound annoying but I've been at this for a few days now and I'm really struggling
     
  7. MaxwellTan

    MaxwellTan

    Unity Technologies

    Joined:
    Mar 3, 2022
    Posts:
    75
    Hi anikataylor, you want to consider looking into merging your sprite first, hope it help

     
    anikataylor likes this.
  8. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    i dont think this is valid for this situation since there are probably too many frames of animation

    I think you have to create a 3D quad, like a rectangle with the shape of your character frame, with a mesh renderer and drag the render texture into it
     
    MaxwellTan and anikataylor like this.
  9. anikataylor

    anikataylor

    Joined:
    Jan 15, 2022
    Posts:
    44
    That was it! Thank you so much haha, in my child game object for the player I added a Mesh Renderer, attached the material with the modified shader graph that uses the player render texture from the new camera, then added a Quad mesh filter, messed around with the scaling a bit + I've got a fully working outline!
     
    MaxwellTan and karderos like this.