Search Unity

Question Parts of my model that are scaled at 0 in some animations never show up in Unity

Discussion in 'Animation' started by Yorsh, Dec 11, 2023.

  1. Yorsh

    Yorsh

    Joined:
    Feb 17, 2015
    Posts:
    6
    Hi,
    This is probably very easy to fix, or have multiple workarounds but I can't figure this out for some reasons.

    I have this chicken model, with a few animations. In one of them, I want to make it come out of its egg. The egg being part of the chicken mesh, with 2 (yellow) bones. I simply scale those 2 to 0 in all other animations, where the egg should not be visible. It works great in Blender. In Unity, the hatching animation plays but the egg stays invisible (the egg bones scale are animated correctly in the armature in hierarchy). I'm assuming Unity is doing some optimization, "oh look, those vertices don't seem to do anything, let's ignore them forever".

    I've tried:
    to turn off Weld Vertices and Optimize Mesh in the mesh import settings. Also I have considered using blend shapes instead but I don't think I can animate it directly in blender with the rest of the animation. I can't really scale the egg to 0.01 or simply move the egg below the ground instead, as I use outline shaders and see through in the game.
    ChickenHatching.gif
     
  2. AAAAAAAAAE

    AAAAAAAAAE

    Joined:
    Jun 8, 2013
    Posts:
    100
    Is the egg under a layer tag that the game camera filters ?
     
  3. Yorsh

    Yorsh

    Joined:
    Feb 17, 2015
    Posts:
    6
    No, it's the same mesh as the chicken.
    upload_2023-12-13_16-33-13.png

    Those 2 egg bones are simply scaled to 0 most of the time.
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,439
    It's probably a core limitation of the import process. (Whoever wrote it in Unity is probably long gone, and whoever owns that feature now is scared to touch it.)

    One workaround that comes to mind is to correct the animation in LateUpdate(). Author the animation so that the smallest bone scale you use is 0.05 or so, and then in LateUpdate, just scale bones that are shorter than that threshold to zero. The animation will go right back to the authored size in the next frame.
     
    Yorsh likes this.
  5. Yorsh

    Yorsh

    Joined:
    Feb 17, 2015
    Posts:
    6
    Thanks! That's a neat idea. It works nicely and doesn't add any complexity outside a 2 lines script on some bones.

    Here is what I did for anyone wondering: Instead of scaling my bones to 0 in blender, I scaled them to 0.1 (anything small). Then in Unity, I added this script to the bones that I want to hide.
    Code (CSharp):
    1. public class ScaleToZeroIfSmall : MonoBehaviour
    2. {
    3.     void LateUpdate()
    4.     {
    5.         if(transform.localScale.sqrMagnitude < 0.05f)
    6.             transform.localScale = Vector3.zero;
    7.     }
    8. }
     
    halley likes this.