Determine if player is accelerating

Hi folks! I am having some issues determining if my character is accelerating using c++.

I am trying to copy this behavior:

This makes it so when the player stops apply input, 'IsAccelerating" returns false immediately

However, when I try and “GetCurrentAcceleration” this does not exist in C++, I tried to look at the code for the “GetCurrentAcceleration” node, however all it does is return acceleration, and shows no function how it is calculated.

This is my current code, and it returns false only when the character has completely stopped.

fMovementSpeed = Pawn->GetVelocity().Size(); 

        //Is character moving?
        if (fMovementSpeed > 0)
            bIsAccelerating = 1;
            bIsAccelerating = 0;

To determine acceleration you will need to remember the velocity of your character from the previous frame (the last time you got a tick). I think that’s what the movement component is doing for you. In your own class you can achieve the same thing.

Here’s some quick code for you. I turned bIsAccelerating into a function.

So, add a property to your class declaration (in your .h)

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class YOUR_API UYourComponentNameHere: public UActorComponent

    // .... some code

    FVector _previousVelocity = FVector::ZeroVector;

    FVector _currentAcceleration = FVector::ZeroVector;

    bool isAccelerating()
        return _currentAcceleration.Size() > 0.0f;

    FVector currentAcceleration()
        return _currentAcceleration;

Then in your TickComponent (in your .cpp):

// Called every frame
void UYourComponentNameHere::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
    Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

    // calculate the current acceleration
    FVector deltaV = GetOwner()->GetVelocity() - _previousVelocity;
    _currentAcceleration = deltaV / DeltaTime;

    _previousVelocity = GetOwner()->GetVelocity();

   // make sure you call your code after the above

Thanks for the reply! Trying to understand why I cannot call the function though. After looking into the example Third Person C++ code, it almost seems as if I am getting the wrong component? In the example, the code for getting character movement is GetCharacterMovement(); however this is called from the Character class, not the anim class. Is there a way to call GetCharacterMovement from my anim class targeting the character?

In your custom class, which class are you inheriting from? In my example, UYourComponentNameHere is inheriting from UActorComponent.

Timtimmy, the class that I am trying to call this from inherits from AnimationInstance. I am trying to turn my AnimBP Event Graph into C++ code. In BPs, i have these values determined from the characters class, then casted to the character class from the animbp. I was trying to get these values from the character class within the animinstance class

I figured out what I was doing wrong the next day…

I was trying to do this in an AnimInstance class, so I had to include UCharacterMovementComponent into my header file, then:

      APawn* Pawn = TryGetPawnOwner();
     AYourCharacter* Character = Cast<AYourCharacter>(Pawn);
     float Acceleration = Character->GetCharacterMovement()->GetCurrentAcceleration().Size();

I know this does not give me the actual acceleration, rather, it gets the size, either 0, or 2048, if MaxAcceleration is set to 2048.

But this is exactly what I needed. ) Leaving this here for others.