Okay so I have been following a tutorial about making a 2D platformer. Everything was going great until I tried to follow the video of making the Jump attack animation play in mid-air. It works just fine except the trigger for the animator won’t reset properly when I place the code to do so in the script in the part the video creator says to.
Here is my script
It tells the Animator to reset the jump trigger. Before I added that part the trigger would activate when the character left the ground but after I did the animator won’t play the animation anymore is just jump straight to the falling animation when the character starts moving down again.
How do I make it work?
I want to include the project files but I don’t know how to do that.
I didn’t quite understand. Do you have 2 animations for jumping, going up and going down?
If that’s the case, you can switch between them directly.
If you have An attack animation mid air, if my memory isn’t trash, I think I had a similar problem but I fixed it with the exit time and maybe the animation state priority.
Yes I have an animation for jumping and falling. As it stands now when I add the code for the animation trigger for jump to be reset, when the character jumps it does not transition to the jumping animation just stays idle, but when the character starts to fall it does transition to the falling animation.
I haven’t figured out what the error is but I suspect is lies in the portion of the code that is
if (myRigidbody.velocity.y <= 0)
{
foreach (Transform point in groundPoints)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].gameObject != gameObject)
{
myAnimator.ResetTrigger("jump");
myAnimator.SetBool("land", false);
return true;
}
}
}
}
return false;
}
However I can find no error when I go back over the part of the video that covers it.
I still really need help with this.
When I first implemented jumping animations, I also used triggers due to a tutorial. But I found out that it is just not as reliable for my type of game. So I changed it from triggers to bools as such:
*You can ignore the "atkAnimationStallTimer "
And in the animator itself, I had a transitions from ‘idle’ to ‘jumping’, which happend if IsJumping = true
A transition from ‘jumping’ to ‘falling’ when IsFalling = true
And a transition from ‘Idle’ to ‘Falling’ when Falling = true && jumping = false.
This worked for me, and also sets your falling animation when you just walk off a ledge, and some other features I have in my game like dashing, teleporting etc.
If you still want to use triggers, my guess is that the problem is in the animator - exit time, transition time, interruption source, transitions in general.
I tired switching it to a bool but it still doesn’t work. However I figured it wouldn’t because I went onto a video before where it asked to set a variable for a jump attack to false that is a bool and it doesn’t work either.
However if I place the myAnimator.ResetTrigger(“jump”); the handlelayers part after line 81 either in the if (isGrounded) part or the else it works with one of them.
Which is weird because it uses the isGrounded function where the reset of the trigger is supposed to be so it works in code using the isGrounded function but will not in the function itself. Another weird thing is the myAnimator.SetBool(“land”, false); code on line 167 is in the isGrounded function and it works just fine.
Anyone want to take another crack at this?
Again, play with the animator itself. Check the transitions, exit time, interruption source.
If every thing is properly written in code, the problem is there.
If the animator is fine, I’d suggest to just delete all the animator code and rewrite it again, step by step.
The animation works fine untill I place the reset trigger code in that certsin spot. So what you are basically telly me to do is go back through the videoes and rewrite the entire thing because I have gone back ovee the animator part in the videos and found no errors
It’s either the animator, which you said is fine, or the logic in the code. You don’t have to rewrite the entire code, just change it a bit. It’s hard for me to debug when I can’t access everything.
I have a feeling that the IsGrounded method returns true for a frame or two after jumping, resetting the jump trigger. Try delaying the IsGrounded method after jumping with a coroutine.
Thats exactly what I have been thinking too. Does that mean the flaw in the logic has to be within the isGrounded function itself? Because I’ve gone back over that part of the video and gone through it with a fine-tooth comb and I couldn’t find any differences in his code and mine
I’m not familiar with the video, but I think you need to leave the video and work it out your self. Seems like rewatching it again won’t lead you any where.
Try and delay the ground check and see what happens.
Create a bool shouldSwitchTriggers = false, and make the actions of resetting the trigger happen only if shouldSwitchTriggers = true. (I’m not sure if you need the other line of code delayed. Test it)
When you jump, start a coroutine which will switch the bool to true after x seconds, maybe after a couple of frames even, and when grounded set the shouldSwitchTriggers to false again.
Something like that, my code is quite different so I can’t say for sure it works for you.