Search Unity

Animation Window and Script inconsistency...

Discussion in 'Scripting' started by Aedous, Aug 30, 2020.

  1. Aedous

    Aedous

    Joined:
    Jun 20, 2009
    Posts:
    244
    Hey!

    I've tried to find the correct wording to use on google, and can't quite seem to find the correct words to use to explain the situation I'm trying to fix.

    I think that there are some inconsistencies with changing variables on a script in an animation clip, and then checking that variable via script in order to carry out an action. I'm currently using Unity 2018.1.3f1
    I'm using the animation window to determine when to create a hit box for the character (any character), it does this by checking if a boolean value is true and therefore proceeds to create the hit box by calling an animation event, and set's up the hit box depending on the values that are set in the animation.
    The boolean and hit box settings are set up using the animation window, and then I'm using animation events to call a script to create the damage box.

    The problem I'm having is some sort of out of sync problem, where the script returns false but the inspector is showing true.
    The green diamond is when the hit box is enabled, and the red diamond is when the hit box is disabled. The animation event to call the damage box is in between the green and red diamond.


    Compared to the HitBoxController script it's returning the correct values, it's enabled and the size / offset of the hitbox is also set:


    However when I compare it to what the script is picking up on that frame using a debug.log it actually returns it as false:


    This doesn't happen all the time, but happens after a few swings or so, it's quite difficult to debug as it's not a consistent issue that appears.
    Hope that makes some sense to someone out there, I was actually thinking that perhaps because my sample rate is quite high that it causes an out of sync problem?
     
  2. diXime

    diXime

    Joined:
    Oct 2, 2018
    Posts:
    162
    Hello,
    I see you have all 4 events during the 59th second of that minute. Meaning you have these 4 events within that second.
    So that out-of-sync event has some sense to me since the fixed frame time is around .2 seconds. If something slows the fps, one can easily overlap.
    Two ways to try and solve it, or at least make sure of the cause:
    - put that script into FixedUpdate, so that it can't happen any other moment than every .2 seconds
    - Look at https://docs.unity3d.com/Manual/TimeFrameManagement.html to build a workaround if the syncing is the issue.

    Happy Coding to you!
     
  3. Aedous

    Aedous

    Joined:
    Jun 20, 2009
    Posts:
    244
    Hey thanks for the reply, I'm not sure frame rate is the actual issue as I've got it running on a blank scene with 60FPS, no stutter or lag inbetween swings but it still occurs.
    My first thought was perhaps the framerate could have been an issue but I've gone ahead and addressed a lot of frame rate issues in the game and still having the same problem.

    I will try out your FixedUpdate idea to see I have an issues with that...however I'm guessing I may get a similar result.