Using Projectile motion for Jump Arcs

alvarofer0020
Fri, 23 Jul 2021 17:01:31 +0000
Character & Animation

Hello

So im trying to integrate a projectile motion equation for my character jumping mechanics, So jumps and their respective arcs are easier to visualize and tweak, As inspiration from these two videos

BetterJumpExample

So far i alredy got a custom character movement component, based off the UCharacterMovementComponent, i have overriden the PhsyFalling state to run my own falling ( and jumping ) logic, however even following the example in the video 1:1 my jumps are not working as they should ( i.e tweaking JumpFall, JumpPeak and JumpHeight ) does not produce a jump that follows these values

MyCharacterMovementComponent.h

UCLASS()
class TESTPROJECT_API UMyCharacterMovementComponent : public UCharacterMovementComponent
{
	GENERATED_BODY()
public:


	UPROPERTY(EditAnywhere)
		float JumpHeight = 100;

	UPROPERTY(EditAnywhere)
		float TimeToFall = 0.5;

	UPROPERTY(EditAnywhere)
		float TimeToPeak = 0.4;

	UPROPERTY()
	float JumpVelocity = (2.0 * JumpHeight) / TimeToPeak;

	UPROPERTY()
	float JumpGravity = (-2.0 * JumpHeight) / (TimeToPeak * TimeToPeak);

	UPROPERTY()
	float FallGravity = (-2.0 * JumpHeight) / (TimeToFall * TimeToFall);


	float GetCustomGravity();

	virtual void PhysFalling(float deltaTime, int32 Iterations) override;
}

MyCharacterMovementComponent.cpp

void UMyCharacterMovementComponent::PhysFalling(float deltaTime, int32 Iterations)
{
        Velocity.Z += GetCustomGravity() * deltaTime;
return;
}

float UMyCharacterMovementComponent::GetCustomGravity()
{
    if (Velocity.Z > 0)
    {
        return JumpGravity;
    }
    else
    {
        return FallGravity;
    }

}


bool UMyCharacterMovementComponent::DoJump(bool bReplayingMoves)
{
    Launch(FVector(0, 0, JumpVelocity));
     //Assigning Z velocity did nothing
    //Velocity.Z = JumpVelocity;
    return true;
}

JumpGDCTalk

With the current implementation, regardless of what values I set JumpHeight, TimeToFall and TimeToPeak to my jumps tend to be mostly very small or short

I could certainly use some help to figure out where things are going wrong, Thanks!

1 post - 1 participant