Hey guys, I’m currently trying to do a multiple unit selection with GetActorsInSelectionRectangle(). The selection kinda works, but not accurately. Whenever I’m doing the selection rectangle, it gets characters that are not within the bounds of the rectangle itself.
Here are some screen shots (pay attention to the Output Log for Selected characters, they are updated on each call to the DrawHud() function, also notice where my selection rectangle is):
My characters don’t have cameras attached nor colliders that extend this far away from them: http://i.imgur.com/OOSSC7q.png
I tried changing the call to GetActorsInSelectionRectangle() and set bActorMustBeFullyEnclosed to true, but still no change. I’ve been trying to fix this for 3 days now, but I think it’s time to ask for help.
This is my code so far:
In my HUD’s DrawHUD() function:
void AMyHUD::DrawHUD()
{
//Player->bDragging is true when the player presses the left mouse button, false when released
if (Player->bDragging)
{
//Player->InitialMousePosition is a 2D Vector that store's the mouse position in screen coordinates when the player presses the left mouse button (does not update each Tick)
//CurrentMousePosition (also a 2D vector) updates each DrawHUD() call with the current mouse position
DrawRect(FColorList::Green.WithAlpha(80), Player->InitialMousePosition.X, Player->InitialMousePosition.Y,
CurrentMousePosition.X - Player->InitialMousePosition.X, CurrentMousePosition.Y - Player->InitialMousePosition.Y);
//Multiple select only if the player has dragged around the mouse at least 20 units on either axis
if (FMath::Abs(CurrentMousePosition.X - Player->InitialMousePosition.X) >= 20 ||
FMath::Abs(CurrentMousePosition.Y - Player->InitialMousePosition.Y) >= 20)
{
//Player->SelectedCharacters is an array that stores my specific kind of Character
Player->SelectedCharacters.Empty();
GetActorsInSelectionRectangle(Player->InitialMousePosition, CurrentMousePosition, Player->SelectedCharacters);
}
}
}
On my main pawn/player (it is automatically possessed by the player when the game begins):
//Called when the left mouse button is pressed
void ARTSCamera::OnSelectBegin()
{
bDragging = true;
APlayerController* PlayerController = UGameplayStatics::GetPlayerController(this, 0);
if (PlayerController)
{
PlayerController->GetMousePosition(InitialMousePosition.X, InitialMousePosition.Y);
FHitResult OutHitResult;
PlayerController->GetHitResultUnderCursorByChannel((ETraceTypeQuery)ECC_Visibility, true, OutHitResult);
if (OutHitResult.bBlockingHit)
{
ARTSRelatedCharacter* Character = Cast<ARTSRelatedCharacter>(OutHitResult.GetActor());
//If we clicked a character then select that one character. HUD takes care of multiple selection.
if (Character)
{
SelectedCharacters.Empty();
SelectedCharacters.Add(Character);
}
else
{
//if we didn't click a character then deselect them all
SelectedCharacters.Empty();
}
}
}
}
//Called when the left mouse button is released
void ARTSCamera::OnSelectEnd()
{
bDragging = false;
}
I think there must be something wrong with the coordinates, I just feel like the green rectangle and the coordinates that I’m sending to the GetActorsInSelectionRectangle() don’t match.
Any help will be greatly appreciated. Thanks!