Search Unity

Question Scale nodes based on object scale

Discussion in 'Shader Graph' started by lemmons, Jan 12, 2021.

  1. lemmons

    lemmons

    Joined:
    Jun 20, 2016
    Posts:
    68
    New to shader graph and working through some tutorials. In making a health bar with a border using a rectangle node, I can't get the border to stay a consistent size as the object scales. I want the border to be about 10% the size of the object itself but it's looking like this:
    chunky.png

    In my shader graph, I've tried accessing the scale property from the object node and manipulating it in a variety of ways but can't get the end result I'm looking for. I think the closest I got was splitting out the scale object and dropping it's values directly into the W and H of the rectangle, but still no dice. Here's where I last left it. Any ideas?
    Screen Shot 2021-01-12 at 11.34.19 AM.png
     
  2. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    It looks like it is 10% of each height and width – to make it have a fixed width that is equal for top/bottom and left/right, you will have to set the Rectangle width to 1 - (0.1 * (Object Scale.y / Object Scale.x)) and keep Rectangle height to 0.9. This is assuming the health bar is always horizontal and assuming "consistent size" means the border thickness stays the same size relative to the overall size.

    P.S:
    Screenshot 2021-01-12 at 18.36.57.png
     
    Last edited: Jan 12, 2021
    lemmons likes this.
  3. lemmons

    lemmons

    Joined:
    Jun 20, 2016
    Posts:
    68
    @florianhanke Wow, thanks so much! This works great. I was just coming back to post my own screenshot

    I don't understand why you wouldn't have to do the same type of computation for the height as well... but for now I'm free to move on to the next thing. Thanks again!
     
    florianhanke likes this.
  4. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    You basically keep one the thickness you want it at (so 0.9), and use the ratio of scale x/y to calculate the ratio of the other to get the same thickness.
    I wrote "assuming the health bar is always horizontal", because if you make it vertical, then 0.9 on the height is too thick and will look goofy (just set width to 0.9 and flip x/y in the calculation for the height to make it work for a vertical health bar).
    Anyway, my pleasure!
     
    ekimontario02 and lemmons like this.
  5. ekimontario02

    ekimontario02

    Joined:
    Jul 4, 2017
    Posts:
    1
    Is there a way to plug in the sequence (shown above) into both their respective width and height slots and have them scale correctly? Currently, when scaled separately in either or X or Y, the opposing axis' width or height collapses in