I’m learning Unreal and now I’m trying to centre the hub in the middle of the screen, but I can’t. I’ve been trying different configurations but both number are not centred:
This is the code I use to draw the HUD:
void AMyPongHUD::DrawHUD()
{
FVector2D ScreenDimensions = FVector2D(Canvas->SizeX, Canvas->SizeY);
FVector2D PlayerScoreTextSize;
FVector2D AIScoreTextSize;
FVector2D StartTextSize;
FString PlayerScoreString;
FString AIScoreString;
float TextScale = 1.0f;
float ScoreY = 50.0f;
float MidScreen = 0.0f;
// Call the parent's version of DrawHUD
Super::DrawHUD();
// Player's and AI's score string.
PlayerScoreString = FString::FromInt(PlayerScore);
AIScoreString = FString::FromInt(AIScore);
// Player's score text size.
GetTextSize(PlayerScoreString, PlayerScoreTextSize.X, PlayerScoreTextSize.Y, ScoreFont);
// AI's score text size.
GetTextSize(AIScoreString, AIScoreTextSize.X, AIScoreTextSize.Y, ScoreFont);
// Midscreen point
MidScreen = (ScreenDimensions.X / 2.0f);
// Draw score.
//DrawText(PlayerScoreString, FColor::White, MidScreen - MidScreenOffset - PlayerScoreTextSize.X, ScoreY, TextFont, TextScale);
//DrawText(PlayerScoreString, FColor::White, MidScreen - MidScreenOffset, ScoreY, TextFont, TextScale);
//DrawText(AIScoreString, FColor::White, MidScreen + MidScreenOffset, ScoreY, TextFont, TextScale);
//float pointA = (ScreenDimensions.X - PlayerScoreTextSize.X) / 2.0f - MidScreenOffset;
//float pointB = (ScreenDimensions.X + AIScoreTextSize.X) / 2.0f + MidScreenOffset;
float pointA = MidScreen - PlayerScoreTextSize.X - MidScreenOffset;
float pointB = MidScreen + AIScoreTextSize.X + MidScreenOffset;
DrawText(PlayerScoreString, FColor::White, pointA, ScoreY, TextFont, TextScale);
DrawText(AIScoreString, FColor::White, pointB, ScoreY, TextFont, TextScale);
UE_LOG(YourLog, Warning, TEXT("ScreenX %f, MidScreen %f, PlayerScoreX %f, AIScoreX %f, MidScreenOffset %f, PointA %f, PointB %f"), ScreenDimensions.X, MidScreen, PlayerScoreTextSize.X, AIScoreTextSize.X, MidScreenOffset, pointA, pointB);
AMyPongGameStateBase* MyGameState = Cast<AMyPongGameStateBase>(UGameplayStatics::GetGameState(this));
if (MyGameState->GetCurrentState() == EPongStates::EWaitingToStart)
{
GetTextSize(TEXT("PLAY"), StartTextSize.X, StartTextSize.Y, TextFont);
DrawText(TEXT("PLAY"), FColor::White, (ScreenDimensions.X - StartTextSize.X) / 2.0f, (ScreenDimensions.Y - StartTextSize.Y) / 2.0f, TextFont, TextScale);
}
}
All the commented code are the tests I did to try to centre without success.
What am I doing wrong?
Maybe the problem is with the camera, but I don’t think so because the paddles and the ball are in the correct position.
You can find all the code [here][2].