CalculateDirection function/node doesn't work in Animation Blueprint

Greetings, I’ve run into a problem into which I’ve spent around 6 hours without a solution, and after various tests my conclusion is that the function/node CalculateDirection doesn’t work properly inside Animation Blueprints.

For context, I’m trying to make an enemy character (AI Controller) move in circles around the player, while playing a strafe animation. The movement is working correctly, but the animation don’t match it.

I’ve already set up a blendspace which controls the logic of the animation, which can be seen here:

ezgif-2-e12a76c4c9

All the animations blend correctly given the speed and direction.
However, I noticed in game that the animation just played forward motion, never using the strafe animation.

I’m using a custom C++ class that inherits from UAnimInstance, and inside a method I update the variables used by the blendspace like this:

void UEnemyCharacterAnimInstance::UpdateAnimationProperties()
{
	if (Pawn == nullptr)
	{
		Pawn = TryGetPawnOwner();
	}

	if (Pawn)
	{
		// Only take into account movement speed when in the ground, disregard speed in Z axis
		FVector Speed = Pawn->GetVelocity();
		FVector LateralSpeed = FVector(Speed.X, Speed.Y, 0.0f);
		MovementSpeed = LateralSpeed.Size();

		// Determine where the direction is relative to the forward vector
		MovementDirection = UKismetAnimationLibrary::CalculateDirection(Speed, Pawn->GetActorRotation());

		if (Enemy == nullptr)
		{
			Enemy = Cast<AEnemyCharacter>(Pawn);
		}
	}
}

Then, since it isn’t working, I decided to check by logging the obtained values from the Animation Blueprint, using the following logic:

And certainly, it always returns a MovementDirection value of 0.
Since this seemed weird, I thought maybe the C++ implementation was broken, so I tried by doing the same logic directly in blueprints like this (only testing Direction as Speed is working correctly):

Unfortunately, it also logs a value of 0 as a result.

Since I am pretty sure the logic is correct, I calculated and logged the exact same values, but this time directly in the Tick function from the EnemyCharacter, like this:

void AEnemyCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	FVector Speed = GetVelocity();
	FVector LateralSpeed = FVector(Speed.X, Speed.Y, 0.0f);
	float MoveSpeed = LateralSpeed.Size();
	UE_LOG(LogTemp, Warning, TEXT("Speed: %f"), MoveSpeed);

	float MovementDirection = UKismetAnimationLibrary::CalculateDirection(Speed, GetActorRotation());
	UE_LOG(LogTemp, Warning, TEXT("Direction: %f"), MovementDirection);
}

And lo and behold, this time in the log it can be seen that it IS calculating the correct value.
The full log is this:

I’m baffled as to why this happens, I don’t get why the calculated value would be different when called directly from the Character and when called from an AnimInstance/Animation Blueprint.

For now I’ve decided to just leave the animations broken and continue with game logic as I’ve wasted a lot of time in this, but if someone has any ideas as to why this happens and how to fix it, I’ll be extremely happy :grinning:

if not, at least maybe someone with my same problem can see this and save themselves hours of frustration.

Cheers!

I just ran into strange behavior myself after switching from using the UAnimInstance’s own CalculateDirection() function to the one in UKismetAnimationLibrary. If your still using 5.0 like me, the solution is to use the old CalculateDirection function the UAnimInstance.

If you have upraded to a newer version of UE could you please tell me if that issue persists? Wondering for myself.