Search Unity

Question [SOLVED] How can I connect varying cast times to a specific animation frame?

Discussion in 'Scripting' started by GrayedFox, Nov 30, 2020.

  1. GrayedFox

    GrayedFox

    Joined:
    Jan 28, 2014
    Posts:
    70
    Hi all, forgive me as this has likely been solved a thousand times but I am clutching at straws here (first time solo RPG project).

    I know about Animation events which are great and allow me to connect a specific frame to a method call that spawns VFX (particle systems, etc) at the right moment. I also know about connecting a mecanim animation state's playback speed to a multiplier variable (or Mecanim parameter) to speed up or slow down the playback speed of a given state (or states).

    Great!

    Now what I want to do is to do is basically define a spell's cast time (say, 2 seconds) and ensure that after 2 seconds the animation reaches the exact frame that has the attached animation event method.

    I thought I could do this by using the length of each animation divided by it's playback speed (so an animation clip that is 1 second long playing at 0.5 speed would equal 2 seconds, or 1 second long at 1.5 speed is 0.667 seconds long), and then use the animation event's time property to get the exact amount of time in seconds that the animation takes to reach that point.

    Problematically, these numbers don't match up at all!

    To my surprise, despite my calculations of one of my cast animation sequences (cast start, cast casting, cast release -- animation event called about half way through the cast release clip), the resulting sum is nearly 1 second longer than it takes to reach the animation event.

    The math is solid, my presumption at the moment is that the blending between each animation (since one clip technically begins before the clip before it ends) is what causing this behaviour.

    TLDR: am I doing this wrong? Should I instead not use animation events at all and instead drive the animator component from my spell manager (i.e. if I know it takes approximately 1 second for the "cast" animation to reach the critical "cast point", to start playing that animation programatically a second before the spell is cast?)

    Thanks for reading this far in and feedback is very welcome
     
  2. GrayedFox

    GrayedFox

    Joined:
    Jan 28, 2014
    Posts:
    70
    Solved this by simply doing N / FPS on the target animation (N being the target frame one wants to cast an animation on) - this gives the exact amount of time it takes to reach that target frame in seconds. Transitioning from the "preparing to cast" animation into the "cast spell" animation happens as soon when:
    spellCastTime - currentCastTime <= transitionTime