Hello, I recently followed Alan Noon’s tutorial for creating a 2d side scroller. However this used a blueprint and I wanted to instead write it in C++, which I am new to. I’ve had some real trouble with changing the animation when the player jumps. I’ve tried using the ACharacter::IsJumping and bPressedJump and PlayerVelocity.Z > 0. I’m now completely stumped.
I’ve searched around and couldn’t find anything so I thought I’d post here.
Current code:
void AAdventureCharacter::IsJumping()
{
const FVector PlayerVelocity = GetVelocity();
if (PlayerVelocity.Z > 0.0f)
{
state = jumping;
UpdateAnimation();
}
else if (&ACharacter::OnLanded)
{
state = stance;
UpdateAnimation();
}
}
Update Animation:
// Are we moving or standing still?
UPaperFlipbook* DesiredAnimation;
if (PlayerSpeed > 0.0f)
{
DesiredAnimation = RunningAnimation;
}
else if (state == jumping)
{
DesiredAnimation = JumpAnimation;
}
else
{
DesiredAnimation = IdleAnimation;
}
GetSprite()->SetFlipbook(DesiredAnimation);
}
Here state in an Enum.
Sorry if this has already been posted, I’ve looked through a lot of posts and couldn’t find an answer.
Try using GetCharacterMovement()->IsFalling() to check if your character is falling/jumping.
I know this questions was posted almost a year ago. I was having a similar problem and thought I should post how I solved it. However, I’m just starting to use ue so there could be a better way (without using blueprints).
I see many solutions suggest to rely on IsFalling, but probably that’s not a good option because state machine could confuse when you fall of a hill and play JUMP instead of something related to a fall animation. It seems there is no real solution to get if the player is jumping, so for this you know that the space bar was pressed, store your own variable for it, and you can make it false when not IsFalling(), that will do the trick.
Well you can always trigger some kind of falling State when ever the Z Velocity exceeds a threshold.
for simple jumping I think GetCharacterMovement()->IsFalling() is the best option like alexh174 said.