I have a little land animation in my 2d platformer where the player kinda squishes like he is made of jello. however, i cant figure out how to implement this via code.
how can i figure out when my player JUST landed?
I have a little land animation in my 2d platformer where the player kinda squishes like he is made of jello. however, i cant figure out how to implement this via code.
how can i figure out when my player JUST landed?
This is sort of thing is where the state pattern, or a finite state machine, comes in handy, as described here: State · Design Patterns Revisited · Game Programming Patterns
State machines generally have Enter and Exit methods that get called when swapping states. If you were to use a state machine for your player controller, your ‘Falling’ state can then play your landing animation on it’s Exit method.
There’s boat loads of tutorials out there for how to do State Machines, though, ironically, the one on the Unity Learn website was the worst of the lot.
So if you have jumping implemented, then I’m sure you have some way of knowing whether the player is touching the ground or not (some kind of isGrounded check). All you have to do is store that value in a variable every frame. Whenever isGrounded is true and your stored value is false, then that means the player must have hit the ground in that exact frame.
Wouldn’t that will play the animation again and again?
No. When kdgalla writes this:
It’s a repeating cycle:
You read the new
You compare with previous and act (only when NOW is true and PREVIOUS is false)
You write down the previous (from the new)
It’s an EXTREMELY common construct in games and software in general.
It is called edge-trigger detection, or transition detection