I am creating a top down shooter but I can’t get the player to rotate via C++ code. It works just fine with blueprints but not in C++. I think I am translating the blueprint code wrong. I will post my code in blueprints and C++ along with video.
My Blueprint code
My C++ code
void ATestController::ControllerAimPressed(const FInputActionValue& Value)
{
FVector2D InputValue = Value.Get();
FRotator RightStickRotation;
FRotator NewRotation;
FVector WorldDirection = FVector(InputValue.Y, InputValue.X, 0.0f);
// Normalize world direction
WorldDirection = WorldDirection.GetSafeNormal();
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("WorldDirection: X = %f, Y = %f, Z = %f"), WorldDirection.X, WorldDirection.Y, WorldDirection.Z));
// Add movement input
TestCharacter->AddMovementInput(WorldDirection, 0.0f);
// Create a rotation from axes
FMatrix RotationMatrix = FRotationMatrix::Make(RightStickRotation);
WorldDirection = RotationMatrix.GetUnitAxis(EAxis::X);
NewRotation = UKismetMathLibrary::MakeRotationFromAxes(WorldDirection, FVector::RightVector, FVector::UpVector);
if (FMath::Abs(InputValue.X) != 0.0f || FMath::Abs(InputValue.Y) != 0.0f)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, FString::Printf(TEXT("InputValue: X = %f"), InputValue.X));
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("InputValue: Y = %f"), InputValue.Y));
TestCharacter->SetActorRotation(RightStickRotation);
TestCharacter->bIsAiming = true;
TestCharacter->bUseControllerRotationYaw = false;
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, FString::Printf(TEXT("bIsAiming: %d"), TestCharacter->bIsAiming));
}
else
{
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("NewRotation: Pitch = %f, Yaw = %f, Roll = %f"), NewRotation.Pitch, NewRotation.Yaw, NewRotation.Roll));
TestCharacter->SetActorRotation(NewRotation);
// Update RightStickRotation
RightStickRotation = NewRotation;
}
}
Blueprint connected
Blueprint Disconnected
I can’t figure out why it will not rotate when using C++
Thanks in advance.