控制鼠标单击的物体

控制鼠标单击的物体 

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);  
            }  
        }  

    }  
}  

 

posted on 2017-03-16 18:58  寒流‘  阅读(166)  评论(0编辑  收藏  举报

导航