I am building 2d game and I am using Unity Animation which apparently causes some problems. Right now I cannot make my character to hit (play animation “JumpAttack”) while he is jumping. Code seems to be fine and all the parameters and animations are fine as well… Could you guys help me to find the solution? I attached the screenshots.
The second issue is my character while he is walking (playing animation “player_run”) if I press attack (animation “player_attack”) he slides a little bit, when actually he should’t move. I thought I prevented to him to move when he plays attack animation right after run animation…
The best way to debug animation problems is to use the Animator window, then you can see the state which it is currently in and you can change parameters yourself. I usually turn off the script that control the player and and test all states by setting the Animation Parameters to appropriate values, then it is possible to follow the state changes in the Animator Window and in the Game Window.
The problem with the attack animation, does it have the exact same pivot as the run animation? Otherwise it will look as though it moves a bit. Also, make sure that there are no overlaps between the transitions, so that not movement from the previous animation goes on into the start of the new animation. Turn off Fixed Duration and set Transition Duration and Offset to zero. This should also be debuggable using the technique I suggested above.
Thanks for the reply!
I can press a Boolean (jumpAnimation) in Animator while game is running and see the animation. But I cannot trigger it within the code, which I thought I did. If I run the game with script as usual the box with JumpAttack in Animator never is triggered and pressing boolean isn’t helping.
If my character is standing and idling and I press Attack he plays the Attack animation but doesn’t move, but once I press Run and Attack he slides for a half a second. It feels like he is still moving for a little bit even when I just press Run. When I press Run shortly and then press Attack he doesn’t slide, but if I press Run for a half a second and then Attack he slides. Its like all the key pressings are accumulating somewhere and running the code when they shouldn’t, if you know what I mean…
So, something is resetting the jumpAnimation? Try to add a Debug.Break() directly after you do you SetBool(“jumpAnimation”, true). That way Unity will pause and you’ll be able to see if it is at least triggered for a frame.
Turn down the speed of the animations (click on the animation clips and change the Speed value to something quick small). That way you’ll be able to observe the transition i slow motion and you’ll be able to see if it actually moves or not.
I managed somehow to trigger “JumpAttack”, but it’s triggered somehow only at the end of “TakeOff” animation and somewhere in “Landing” animation. It doesn’t matter how many times I press “JumpAttack” its triggered in very strange phases and only a few times. I could’t trigger more than two times per jump, when there are a lot of time to trigger it 10 times probably. Sometimes when “JumpAttack” is triggered it goes to animation “TakeOff” or even “Default State” and then to “Landing” and again “Landing”. Therefore I can see a lot of twitching moves, when an animation is started and after a few milliseconds is started again.
I made Run and Attack animation it slower but I couldn’t see anything new. He still slides.
Does this mean that the Transition between TakeOff and JumpAttack has Has Exit Time set?Otherwise it shouldn’t wait until the end of the clip before transitioning. If you select the transition, there is a scrubber on the Time Line, if you drag this does the transition play as it should (you can see what happens in the Preview window (which also has a scrubber)).
If I look at the image you posted, I would assume that JumpAttack only should transition back to Landing. So it is very strange the it winds up in TakeOff or anything else. Wouldn’t it have to go to from JumpAttack to Landing, then to Default Stat and and finally TakeOff, that is a lot of transitions that are triggered more or less immediately. I would assume that a number of Parameters must be changed at once for this mass transition to occur?
It is probably possible to see what happens with the Run and Attack animation if you use the scrubber on the Transition Timeline.
Darn, it’s hard to debug without actually having the access to the Animator in question
public Animator anim;
public List<string> AniList = new List<string>();
void Awake()
{
anim = GetComponentInChildren<Animator>();
}
// Make the name list of parameters
public void RefreshAniControllerContent()
{
AniList.Clear ();
AnimatorControllerParameter param;
for (int i = 0; i < anim.parameters.Length; i++)
{
param = anim.parameters[i];
if (param.type == AnimatorControllerParameterType.Bool)
{
AniList.Add(param.name);
}
}
}
public void SetAnimation(string Name, float Speed)
{
foreach (string Anis in AniList)
{
if (Anis == Name)
{
anim.SetBool(Anis,true);
if (Speed != 0)
{
anim.speed = Speed;
}
}
else
{
anim.SetBool(Anis,false);
}
}
}
Its a script that will 100% trigger you animatiom (it will collect all the bool states for you). You just call it by SetAnimation(name) and it will 100% play. Try it No overhead and no issue. From the Ani list oyu can see all your animation from the controller
I added all of these but nothing has changed. There are no errors, so script works fine. I have feelings that its because of the animation. I can see that sometimes when I press Attack this doesn’t trigger Attack animation, and sometimes it triggers only in some weird phases, like almost at the end of TakeOff animation. When it triggers Attack animation sometimes it goes to Default state and then to Landing which is completely against what I build in Animator. Magic! I would expect some delay in animation if I had some transition, but I specifically unchecked any transition so it should go immediately from one animation to another. BTW animation works as it should only when character walks and I press attack (except it slides, but at least animations are triggered as they should). So the problem only in jumping.
Yes i am pretty sure its your Animator controller that is the issue, no codes can fix that, make sure your connection tweens are linked correclty with animations. They all need a condition state.
I looked through your code, and I get this nagging feeling that your Animation Parameters aren’t always reset properly. Which could lead to the problems you mentioned earlier (that is, jumpAttack transitioning to “random” states). If some parameter isn’t reset, then a transition to a new state could lead to an immediate transition to the next state.
Adding some Debug.Log(“”; Debug.Break(); Would show you what the state was supposed to be in that frame, and give you the opportunity too look thorough the current parameter settings before stepping to the next frame. Then you should be able to see if you are in the correct node and if the right parameters are set. Keep on stepping frame by frame too see that both your code and your transitions keep in sync as they are supposed to do.