I currently have a sprite-based animation in which the player is running. I also have another animation where the player is running and holding a gun out and shooting, that plays at the exact same speed as the player running without the gun. The catch is, I'm using Animator.Play();
What I want is to start the player running with gun animation at the frame that the player running animation is currently at. For instance, if player running animation is at frame 20, and the player presses the shoot button, play the running with gun animation at frame 20.
Here is a rough example of my code so far:
if (!shooting) {animator.Play("PlayerRunning");}
if (shooting) {animator.Play("PlayerRunningShoot");}
I think the best example would be one of the NES megaman games' megaman shooting animataion, where when you press 'B' while running, his arm will immediately pop-up to shoot.
If any of this is confusing on what I am asking, let me know.
It looks like actually using Animation Layers would be way more interessting for you! I can not recreate a complete manual here - look into the docs and this tutorial.
However, Animator.Play has an optional parameter
so you can start the new animation with the offset the current animation is at
you can use
Animator.GetCurrentAnimatorStateInfo
to get the current state and thenormalizedTime
the current clip is atand
Animator.GetCurrentAnimatorClipInfo
to get the information (e.g. length) of the clip.And either reference your target clip (I would prefer that) or you can use
Animator.runtimeAnimatorController
to get allAnimationClip
s and than useLinQ FirstOrDefault
to find the one with the target name:(typing this in on my smartphone so no warranties)
You could also create a smooth transition on runtime using
Animator.CrossFade or Animator.CrossFadeInFixedTime