void AMyCharacter::PossesClicked() { APlayerController* PlayerController = Cast<APlayerController>(GetController()); if (PlayerController != nullptr) { // Get the coordinates of the mouse from our controller float LocationX; float LocationY; PlayerController->GetMousePosition(LocationX, LocationY); // Do a trace and see if there the position intersects something in the world FVector2D MousePosition(LocationX, LocationY); FHitResult HitResult; const bool bTraceComplex = false; if (PlayerController->GetHitResultAtScreenPosition(MousePosition, ECC_Visibility, bTraceComplex, HitResult) == true ) { // If the actor we intersected with is a controller posses it APawn* ClickedPawn = Cast<APawn>(HitResult.GetActor()); if( ClickedPawn != nullptr ) { // Unposses ourselves PlayerController->UnPossess(); // Posses the controller we clicked on PlayerController->Possess(ClickedPawn); } } } }
时间: 2024-12-28 10:42:25