Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Broken animation in Windows build, fine in play mode

Discussion in 'Animation' started by schinkenpapst_unity, Oct 17, 2023.

  1. schinkenpapst_unity

    schinkenpapst_unity

    Joined:
    May 7, 2023
    Posts:
    7
    Hi Guys,

    I'm stuck for days with an issue on animating my character. In play mode it works perfect, unfortunately whenever I build and start it the results looks like all my characters are trapped in a twister.

    I have an object called player with 5 child objects body, hair, eyes, outfit and accessory. The user should be able to change the look, so I have 5 different images rendered in a specific order to ensure the character looks as it should.

    Each different visual object such as body, hair or accessory has 48 different sprites, as each animation consists of 6 sprites * 4 for each direction * 2 for standing and walking.

    This way I have e.g. 27 hair styles, which additionally have 7 hair colors each.

    This issue is happening for each of these objects, so I use the body as the simplest example.

    Initially I had created an animation controller and used animations triggered with keyboard inputs to decide if the character is standing or walking, but then I had this issue, so I removed the animations and decided to update the sprites in each FixedUpdate() call instead.

    Again working fine in play mode, but not in the built game.

    The player looks like each part constantly receives different inputs, resulting in a body looking left, hair from the right looking, equipment from the up looking and so on.

    This changes several times per second so it looks the character is in a twister.

    The animationIds are ordered and should loop only in a specific way, e.g. 0-5 = looking down, 6-11 looking left and so on.

    I have another script called Move which uses keyboard inputs to calculate the offset and loop through the 6 sprites. If I'm e.g. walking right it will set it's animationId which is referenced in each of the scripts to an integer looping through [42,43,44,45,46,47] every 0.1f seconds.

    I have added a Text Mash Pro GUI field to show the current animationId. It's correct, always resulting in the correct number, in play mode and the built game.

    Please take a look at my code, maybe you have an idea

    Code (CSharp):
    1. public class Body : UpdateReceiver
    2. {
    3.     private int bodyId = 0;
    4.     private int animationId = 0;
    5.  
    6.     private Sprite[] spriteList = new Sprite[48];
    7.     private int lastBodyId = 0;
    8.     private int lastAnimationId = 0;
    9.     public bool ui = false;
    10.  
    11.     protected void Start()
    12.     {
    13.         bodyId = gameObject.GetComponentInParent<Person>().playerLook._bodyId % PersonStatics.bodyList.Length;
    14.         AsyncOperationHandle<Sprite[]> spriteHandle = Addressables.LoadAssetAsync<Sprite[]>("Assets/Tilemaps/Character/Body/" + PersonStatics.bodyList[bodyId % PersonStatics.bodyList.Length] + ".png");
    15.         spriteHandle.Completed += LoadSpritesWhenReady;
    16.         lastBodyId = bodyId;
    17.     }
    18.  
    19.     protected void LoadSpritesWhenReady(AsyncOperationHandle<Sprite[]> handleToCheck)
    20.     {
    21.         if (handleToCheck.Status == AsyncOperationStatus.Succeeded)
    22.         {
    23.             spriteList = handleToCheck.Result;
    24.             if (ui)
    25.             {
    26.                 GetComponent<Image>().sprite = spriteList[animationId];
    27.             }
    28.             else
    29.             {
    30.                 GetComponent<SpriteRenderer>().sprite = spriteList[animationId];
    31.             }
    32.         }
    33.     }
    34.  
    35.     protected void FixedUpdate()
    36.     {
    37.         bodyId = gameObject.GetComponentInParent<Person>().playerLook._bodyId % PersonStatics.bodyList.Length;
    38.         animationId = gameObject.GetComponentInParent<Move>().animationId % 48;
    39.         if (lastBodyId != bodyId)
    40.         {
    41.             AsyncOperationHandle<Sprite[]> spriteHandle = Addressables.LoadAssetAsync<Sprite[]>("Assets/Tilemaps/Character/Body/" + PersonStatics.bodyList[bodyId % PersonStatics.bodyList.Length] + ".png");
    42.             spriteHandle.Completed += LoadSpritesWhenReady;
    43.             lastBodyId = bodyId;
    44.         }
    45.         else if(animationId != lastAnimationId)
    46.         {
    47.             if (ui)
    48.             {
    49.                 GetComponent<Image>().sprite = spriteList[animationId];
    50.             }
    51.             else
    52.             {
    53.                 GetComponent<SpriteRenderer>().sprite = spriteList[animationId];
    54.             }
    55.         }
    56.         lastAnimationId = animationId;
    57.     }
    58. }
    Thanks in advance!

    Regards,
    Christian
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,273
    maybe video clip to compare editor vs build would help.

    check also editor.log for possible error messages
    https://docs.unity3d.com/Manual/LogFiles.html

    check if build and editor have same quality settings,
    check if vsync makes difference.
     
  3. schinkenpapst_unity

    schinkenpapst_unity

    Joined:
    May 7, 2023
    Posts:
    7
    Hi mgear,

    Thanks for the quick response :)

    I checked all logs, but unfortunately I was not able to find any suspicious errors.

    Both quality settings are the same and modifying vsync didn't have an effect.

    This is how it looks in the editor: https://serverless-works--dev.s3.eu...nity+2022.2.19+_DX11_+2023-10-18+15-59-51.mp4

    vs the whirlwinds in the built game: https://serverless-works--dev.s3.eu-central-1.amazonaws.com/City+2023-10-18+16-03-02.mp4

    The white numbers indicate the animationId and it looks the same for both.

    Any further idea what might be wrong?

    Best Regards,
    Christian
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,273
  5. schinkenpapst_unity

    schinkenpapst_unity

    Joined:
    May 7, 2023
    Posts:
    7
    Hi mgear,

    Thanks, I just tried your suggestions. Disabling dynamic batching or changing the sprite packer (was v1 always, tried disabled and v2 always) didn't change. It also doesn't use a custom shader.

    What kind of input are refering to? My user inputs to move?

    This is my script move which is used to move 64px after each input and it also updates the public animationId which is reused in my other scripts to always animate the correct sprite. I also tried to store only the activityType in a public field and do the animationId in the other scripts, but this didn't help as well:

    Code (CSharp):
    1. using System;
    2. using TMPro;
    3. using UnityEngine;
    4. using UnityEngine.Tilemaps;
    5. using static PersonStatics;
    6.  
    7. public class Move : UpdateReceiver
    8. {
    9.     public Tilemap ground;
    10.     public Tilemap blocked;
    11.     private Vector2 targetPosition;
    12.     public float speedX = 1f;
    13.     public float speedY = 1f;
    14.     private Vector2 movement;
    15.     private bool animationSet = false;
    16.     public float initX = 0;
    17.     public float initY = -1;
    18.     private float prevX = 0;
    19.     private float prevY = 0;
    20.     private Vector2 moveDelta;
    21.     public bool player = false;
    22.     public string remoteControl = "";
    23.     private string instructions = "";
    24.     public PersonActivity initialActivity = PersonActivity.lookDown;
    25.     private PersonActivity currentActivity;
    26.     public int animationId;
    27.     private float timeDelta = 0;
    28.     public float animationSpeed = 0.1f;
    29.  
    30.     //private BoxCollider2D boxCollider;
    31.     private bool blockUp = false;
    32.     private bool blockDown = false;
    33.     private bool blockLeft = false;
    34.     private bool blockRight = false;
    35.     private Vector2 raySource;
    36.     private bool locked;
    37.  
    38.  
    39.  
    40.     private void Awake()
    41.     {
    42.         player = gameObject.GetComponentInParent<Person>().player;
    43.         targetPosition = new Vector2(transform.position.x,transform.position.y);
    44.         transform.position = targetPosition;
    45.         // boxCollider = GetComponent<BoxCollider2D>();
    46.         currentActivity = initialActivity;
    47.     }
    48.  
    49.     private void Animate()
    50.     {
    51.         timeDelta += Time.deltaTime;
    52.         int framesPassed = Convert.ToInt32(Math.Floor(timeDelta / animationSpeed));
    53.         animationId = framesPassed % 6 + GetAnimationOffset(currentActivity);
    54.         if (player)
    55.         {
    56.             GameObject.Find("AnimationIdText").GetComponent<TextMeshProUGUI>().text = animationId.ToString();
    57.         }
    58.     }
    59.  
    60.     private void FixedUpdate()
    61.     {
    62.         Animate();
    63.         locked = gameObject.GetComponentInParent<Person>().locked;
    64.         raySource = (Vector2)transform.position + (Vector2.down * 0.5f) + (Vector2.right * 0.5f);
    65.         transform.rotation = Quaternion.AngleAxis(0, Vector3.zero);
    66.         var moving = (Vector2)transform.position != targetPosition;
    67.         if (moving && Vector2.Distance((Vector2)transform.position, targetPosition) <= 0.03f)
    68.         {
    69.             MoveTowardsTargetPosition();
    70.             moving = false;
    71.             switch(currentActivity)
    72.             {
    73.                 case PersonActivity.moveUp:
    74.                     currentActivity = PersonActivity.lookUp;
    75.                     break;
    76.                 case PersonActivity.moveDown:
    77.                     currentActivity = PersonActivity.lookDown;
    78.                     break;
    79.                 case PersonActivity.moveLeft:
    80.                     currentActivity= PersonActivity.lookLeft;
    81.                     break;
    82.                 case PersonActivity.moveRight:
    83.                     currentActivity= PersonActivity.lookRight;
    84.                     break;
    85.             }
    86.         }
    87.         if (remoteControl != "")
    88.         {
    89.             instructions = remoteControl;
    90.             remoteControl = "";
    91.         }
    92.         else if (instructions != null && instructions.Length != 0)
    93.         {
    94.             if (moving)
    95.             {
    96.                 MoveTowardsTargetPosition();
    97.             }
    98.             else
    99.             {
    100.                 SetNewTargetPositionFromControl();
    101.                 instructions = instructions.Substring(1);
    102.             }
    103.         }
    104.         else
    105.         {
    106.             if (player)
    107.             {
    108.                 movement.x = Input.GetAxisRaw("Horizontal");
    109.                 movement.y = Input.GetAxisRaw("Vertical");
    110.  
    111.  
    112.                 if (moving)
    113.                 {
    114.                     MoveTowardsTargetPosition();
    115.                 }
    116.                 else
    117.                 {
    118.                     blockUp = Physics2D.Linecast(raySource, raySource + (Vector2.up * 1.4f), LayerMask.GetMask("Blocking"));
    119.                     blockDown = Physics2D.Linecast(raySource, raySource + (Vector2.down * 1.4f), LayerMask.GetMask("Blocking"));
    120.                     blockLeft = Physics2D.Linecast(raySource, raySource + (Vector2.left * 1.4f), LayerMask.GetMask("Blocking"));
    121.                     blockRight = Physics2D.Linecast(raySource, raySource + (Vector2.right * 1.4f), LayerMask.GetMask("Blocking"));
    122.                     SetNewTargetPositionFromInput();
    123.                 }
    124.             }
    125.         }
    126.     }
    127.  
    128.     private void MoveTowardsTargetPosition()
    129.     {
    130.         transform.position = Vector2.MoveTowards(transform.position, targetPosition, speedX * Time.deltaTime);
    131.     }
    132.  
    133.     private void SetNewTargetPositionFromInput()
    134.     {
    135.         if (!locked)
    136.         {
    137.             float x = Input.GetAxisRaw("Horizontal");
    138.             float y = Input.GetAxisRaw("Vertical");
    139.             if ((x != 0 && x != prevX) || (y != 0 && y != prevY))
    140.             {
    141.                 animationSet = true;
    142.             }
    143.             prevX = x;
    144.             prevY = y;
    145.             moveDelta = new Vector2(x * speedX, y * speedY);
    146.             if (y == 1 && !blockUp)
    147.             {
    148.                 currentActivity = PersonActivity.moveUp;
    149.                 targetPosition += Vector2.up;
    150.             }
    151.             else if (y == -1 && !blockDown)
    152.             {
    153.                 currentActivity = PersonActivity.moveDown;
    154.                 targetPosition += Vector2.down;
    155.             }
    156.             else if (x == 1 && !blockRight)
    157.             {
    158.                 currentActivity = PersonActivity.moveRight;
    159.                 targetPosition += Vector2.right;
    160.             }
    161.             else if (x == -1 && !blockLeft)
    162.             {
    163.                 currentActivity = PersonActivity.moveLeft;
    164.                 targetPosition += Vector2.left;
    165.             }
    166.             else
    167.             {
    168.                 animationSet = false;
    169.                 switch (currentActivity)
    170.                 {
    171.                     case PersonActivity.moveUp:
    172.                         currentActivity = PersonActivity.lookUp;
    173.                         break;
    174.                     case PersonActivity.moveDown:
    175.                         currentActivity = PersonActivity.lookDown;
    176.                         break;
    177.                     case PersonActivity.moveLeft:
    178.                         currentActivity = PersonActivity.lookLeft;
    179.                         break;
    180.                     case PersonActivity.moveRight:
    181.                         currentActivity = PersonActivity.lookRight;
    182.                         break;
    183.                 }
    184.             }
    185.         }
    186.         else
    187.         {
    188.             animationSet = false;
    189.             switch (currentActivity)
    190.             {
    191.                 case PersonActivity.moveUp:
    192.                     currentActivity = PersonActivity.lookUp;
    193.                     break;
    194.                 case PersonActivity.moveDown:
    195.                     currentActivity = PersonActivity.lookDown;
    196.                     break;
    197.                 case PersonActivity.moveLeft:
    198.                     currentActivity = PersonActivity.lookLeft;
    199.                     break;
    200.                 case PersonActivity.moveRight:
    201.                     currentActivity = PersonActivity.lookRight;
    202.                     break;
    203.             }
    204.         }
    205.     }
    206.  
    207.     private void SetNewTargetPositionFromControl()
    208.     {
    209.         if (instructions[0].Equals("u".ToCharArray()[0]))
    210.         {
    211.             targetPosition += Vector2.up;
    212.         }
    213.         else if (instructions[0].Equals("d".ToCharArray()[0]))
    214.         {
    215.             targetPosition += Vector2.down;
    216.         }
    217.         else if (instructions[0].Equals("r".ToCharArray()[0]))
    218.         {
    219.             targetPosition += Vector2.right;
    220.         }
    221.         else if (instructions[0].Equals("l".ToCharArray()[0]))
    222.         {
    223.             targetPosition += Vector2.left;
    224.         }
    225.         else if (animationSet)
    226.         {
    227.             animationSet = false;
    228.         }
    229.     }
    230. }
    231.  
    I also have this in a static class PersonStatics:

    Code (CSharp):
    1. public enum PersonActivity
    2.     {
    3.         lookUp,
    4.         lookDown,
    5.         lookLeft,
    6.         lookRight,
    7.         moveUp,
    8.         moveDown,
    9.         moveLeft,
    10.         moveRight,
    11.     }
    12.  
    13.     public static Int32 GetAnimationOffset(PersonActivity activity)
    14.     {
    15.         switch (activity)
    16.         {
    17.             case PersonActivity.lookUp:
    18.                 return 6;
    19.             case PersonActivity.lookDown:
    20.                 return 18;
    21.             case PersonActivity.lookLeft:
    22.                 return 12;
    23.             case PersonActivity.lookRight:
    24.                 return 0;
    25.             case PersonActivity.moveUp:
    26.                 return 30;
    27.             case PersonActivity.moveDown:
    28.                 return 42;
    29.             case PersonActivity.moveLeft:
    30.                 return 36;
    31.             case PersonActivity.moveRight:
    32.                 return 24;
    33.             default: return 0;
    34.         }
    35.     }
    These are my Player settings:

    upload_2023-10-18_20-37-18.png
    upload_2023-10-18_20-37-53.png
    upload_2023-10-18_20-38-37.png

    Does this help?

    Thanks in advance!

    Best Regards,
    Christian
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,273
    not sure whats happening..
    some notes,
    - Input.getaxis shouldn't be inside FixedUpdate (it shouldnt work there properly)
    - try printing out if somehow input values are getting set in build?
    - do you unsubscribe from all events when done
    - theres input.getaxis in 2 places?

    or,
    if you disable those input.getaxis lines or set to 0, is the character still bugged?
    if you set monitor refresh rate to 60, any difference?
     
    Last edited: Oct 19, 2023
  7. schinkenpapst_unity

    schinkenpapst_unity

    Joined:
    May 7, 2023
    Posts:
    7
    Hi mgear,

    Sorry for the delay, had some busy days at work.

    Thanks for your findings, removed one input.getAxis.

    I now also replaced input.getAxis with player inputs from the Rewired package, didn't help.

    Setting movement x and y to 0 doesn't change a thing, still twister.

    My monitor already is 60fps.

    When setting the animationId to 0 the animation stops but the person has the correct sprites in use and there is no twister.

    I added additional text boxes for displaying the animationId and now it gets even more weird.

    In the editor the debugged values look strange, as if they would not change based on the activity, they only look correct when looking down (default state)

    upload_2023-10-25_21-37-39.png

    And in the windows built it's exactly the opposite, there the values are always correct, but the displayed sprites look totally different.(body looking left, outfit moving left, eyes in between looking left and looking right, accessory looking down, hair looking down in this screenshot)

    upload_2023-10-25_21-36-37.png

    In each script I retrieve the correct animationId like this:

    animationId = gameObject.GetComponentInParent<Move>().animationId;

    Any further ideas what might be the reason?

    Thanks in advance!

    Best Regards,
    Christian
     
  8. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,273
    yup, hard to say at this point..
    need to try to debug & track down what changes those animations.

    could also test runtime inspector, if they would show something in build
    https://github.com/yasirkula/UnityRuntimeInspector
    https://github.com/ManlyMarco/RuntimeUnityEditor

    or if all fails, try to rewrite the script in more simpler way, starting from single object maybe.
    (it seems bit complicated for "just" handling movement and animations?)

    its also possible that script execution order matters, if you do things in Awake/Start with many scripts.
     
  9. schinkenpapst_unity

    schinkenpapst_unity

    Joined:
    May 7, 2023
    Posts:
    7
    Hi mgear,

    Thanks for the hint, the Runtime Inspector is great, added it, stopped the animation and manually set the animationId in the move component to different values.

    It seems like every single animationId is fixed to one look, but the sprites are different than in the editor as if they would pick a different one over there. But it still keeps the correct style, just the style is different.

    Therefore I guessed the error is somewhere in my logic where I decide which sprite to use.

    If we take the body as easiest example there we have a static class PersonStatics with this static string array:

    Code (CSharp):
    1. public static string[] bodyList = new string[]
    2.     {
    3.         "Body_01",
    4.         "Body_02",
    5.         "Body_03",
    6.         "Body_04",
    7.         "Body_05",
    8.         "Body_06",
    9.         "Body_07",
    10.         "Body_08",
    11.         "Body_09",
    12.     };
    In the body script I retrieve the current body and load the sprites and add a handler for when it's completed in the start function:

    Code (CSharp):
    1. bodyId = gameObject.GetComponentInParent<Person>().playerLook._bodyId % PersonStatics.bodyList.Length;
    2.         AsyncOperationHandle<Sprite[]> spriteHandle = Addressables.LoadAssetAsync<Sprite[]>("Assets/Tilemaps/Character/Body/" + PersonStatics.bodyList[bodyId % PersonStatics.bodyList.Length] + ".png");
    3.         spriteHandle.Completed += LoadSpritesWhenReady;
    In the handler I assign the results to the sprite list and set the sprite in the sprite renderer to the correct sprite:

    Code (CSharp):
    1. protected void LoadSpritesWhenReady(AsyncOperationHandle<Sprite[]> handleToCheck)
    2.     {
    3.         if (handleToCheck.Status == AsyncOperationStatus.Succeeded)
    4.         {
    5.             spriteList = handleToCheck.Result;
    6.             if (ui)
    7.             {
    8.                 GetComponent<Image>().sprite = spriteList[animationId];
    9.             }
    10.             else
    11.             {
    12.                 GetComponent<SpriteRenderer>().sprite = spriteList[animationId];
    13.             }
    14.         }
    15.     }
    Therefore e.g. if the bodyId is 0 and animationId is 5 then it loads all sprites from Assets/Tilemaps/Character/Body/Body_01.png which is correct, but then it should load the 6th element from the spritelist.

    In editor therefore the sprites are assigned:

    animationId 0 -->Body_01_0
    animationId 1 -->Body_01_1
    ...

    while in the built it's

    animationId 0 -->Body_01_44
    animationId 1 -->Body_01_26
    ...

    So "spriteList = handleToCheck.Result;" returns an ordered list in editor but an unsorted one in the built game.

    So adding another line for sorting solved this issue, now it's finally working :)

    Code (CSharp):
    1. Array.Sort(spriteList, delegate (Sprite x, Sprite y) { return Int32.Parse(x.name.Split("_")[2]).CompareTo(Int32.Parse(y.name.Split("_")[2])); });
    Thank you very much for your support :)

    Best Regards,
    Christian
     
    mgear likes this.